Search code examples
c++configuration-filesconvention

Location of config files in project c++


I was wondering whether there is a convention for storing config files in c++. At the moment I am storing the config files in the same directory as the source code that uses them. When building I make sure via the CMakeLists to copy them to the correct location so I can just access them in a relative way (e.g. "config.cfg" iso "/foo/bar/config.cfg) for convenience.


Solution

  • The practice for config files is non portable and operating system dependent. You have also to ask yourself if your configuration is defined per installation/system or per user.

    In general, it is a very bad idea to store config in the same directory as your execuable, at least once the developpement is finished. In general executables may be shared between several users and should therefore for security reasons be located in directories that are write-protected for everybody but the system administrator.

    For unix/linux, you could for example consider:

    • /etc or a subfolder thereof, if your configuration is per installed system,
    • ~/ if it's user defined configuration. The usual practice would be to start the filename with a dot. This article will tell you more.

    For windows systems, you should consider:

    • the usual approach now, goes to the registry. Of course, this uses the windows api and is fully non portable.
    • a subfolder of C:\ProgramData or C:\Users\All users\AppData\Local if your configuration is per installed system,
    • a subfolder of C:\Users\%USERNAME%\AppData\Local for the users's own configuration. This SO questions shows how to find the right folders.