On Sun, Mar 14, 2010 at 11:29 PM, Reuben Thomas <r...@sc3d.org> wrote: > I have a C program which loads some scripts at runtime. These are > stored in datadir (e.g. /usr/local/share/prog). But I also want to be > able to run the program from its build directory.
We have some similar situations but failed to find one solution working well in all cases. For unit tests, we sometimes (run-time) re-configure the test object. This might be as simple as: static const char *configPath_ = "/usr/local/share/prog"; const char *getConfigPath(void) { return configPath_); } void setTestConfigPath(void) { configPath_ = "."; } If the application has something like: int main(int argc, char *argv[]) { return appXyzMain(argc, argv); } the test can call appXyzMain with an argv containing `--data-dir=.' option. Then there are cases where it seems suited to have a list of directories where to search: const char *paths[] { "./tests/", "/usr/local/share/", "/usr/share" }; Some tools (helper scripts) even `guess' if they run from a sandbox (e.g. by looking if a file Makefile exists in same directory or compare $argv[0] with @prefix@, but all this seems to be tricky and surely not portable). oki, Steffen