Question

I want to open a number of files (log4cxx configs, other logs etc) relative to binary's location.

Unfortunately, both getwd() and getcwd() are giving me the directory, from which I try to run binary at known path, instead of giving me the path where the binary is located (and where the data is).

How to get the app's path to use it with chdir()? Any methods besides argv[0] and without trying to parse /proc/$PID/ (that's not portable enough)?

Was it helpful?

Solution

This is exactly the kind of thing autoconf lives for, and supporting those standard directories is pretty much mandatory if you ever want anyone other than the programmers who wrote your software to use it. Once set up properly, to debug out of your home directory all you have to do is pass a different --prefix= value to configure.

OTHER TIPS

Walk the PATH and find an executable of the same name as argv[0]?

However, it would probably be better to provide the user a way to configure where the data is. An env var or config file or CL parameter or something. It's very frustrating dealing with programs that try to be helpful but are actually just stupid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top