tags:

views:

79

answers:

2

I'm writing a program for Linux in C++, and I need to store some additional data, such as images. Stuff like that is usually in /usr/share on Linux.

The user can decide where to install the software (I'm using CMake), thus I should either use /usr/share, /usr/local/share, /home/theuser/somefolder/share or whatever, depending on where he installed it.

I usually go about doing this by figuring out the absolute path to my binary, cutting the trailing "bin" from the path and replacing it with "share". However, this is quite cumbersome and not the least elegant, so I was wondering how other people did it. I'm using boost, but I can't find any respective functions.

I only need the share directory for this project, but I'd also be interested in how you do this with the etc directory (my approach doesn't quite work there, because the binary can be in /usr/bin while the configuration files are in /etc)

A: 

You could use default directories paths.

Kirill V. Lyadvinsky
I do, /usr/share and /usr/local/share are both default. But I don't want to take the ability of specifying his own path from the user if the build system allows it.
theone
+1  A: 

The build system should pass the desired install location as a define during the build process. So

gcc -DDATA_DIR=/custom/build/location ...

This means that the install location can't be changed after the code is built, but is the only way to be certain that the code knows where to look, without reading that information somewhere at runtime.

KeithB
Should that be `-D\"DATA_DIR=/custom/build/location\"`?
Dan Moulding
Hm, that doesn't sound very flexible, but I think I saw that somewhere before. I'm a bit amazed that there is no plausible method for something like this after all those years.
theone
@Dan Doh, your right.
KeithB