I have a couple projects that depend on additional files, like images or config files.

When compiling and installing a programs like Okular with cmake, I can specify an installation directory and let make install install to that. Somehow, that program will figure out that all its dependencies are within that folder, not withing the main Linux file system.

So far, I had absolute paths, like /etc/config.ini. The problem is that I cannot test the program without it installing it to my system any more.

How can I do that easily in C++ and Python?


C/C++/other languages built with cmake/autotools/...

The usual method is to have the build system either pass the specified installation directory to the compiler as a define flag (-DPREFIX=${CMAKE_INSTALL_PREFX}) or written in a configuration header (#define PREFIX @CMAKE_INSTALL_PREFIX@). This gets compiled in the code, constant values are thus used at runtime.

Note, that it is important to allow the prefix to be changed for the installation, i.e. install the files in different path from what is compiled in the sources. This is used when creating packages and various special cases when the files will eventually be linked or mounted into the location that was compiled in the code. Both cmake and autotools allow such overriding by default.


Here the usual method is simply placing all your data in the directory where your modules are and search for them in sys.path.

  • Regarding Python: So should I install my config file into /usr/lib/python3/dist-packages or just add /etc to my path? I guess the latter makes more sense. – Martin Ueding Sep 27 '13 at 17:15
  • @queueoverflow: /etc definitely makes more sense; I am not sure what is easiest way to make it configurable though as I've only done this in C/C++. Look at whether setuptools have to offer anything. – Jan Hudec Sep 29 '13 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.