@OP: The posts by "r-senior", "dwhitney67" and others are already excellent.
What they are missing a bit is some additional explanation: in Windows programs, it is quite common to have the data files of your application in sub-directories of the directory in which your program is located.
In Linux, this is *not* a typical thing to do. Rather, as most software is installed using package managers, the path to look for data files is hardcoded into the binaries. Then, you don't ever need to look at the current path. As sometimes, you want to install your program to some special location (e.g., when you don't have root rights for installation), this data file location needs to be changed - If you designed your program to be compiled using autotools, they will handle that for you.
Now some commercial software still uses the "Windows"-style approach that you also suggested. Such software is typically installed into the "/opt" branch of your filesystem. You can "find" your executable from the arguments to your program and compute the relative paths of your data files from there. Here is an example in c++:
Code:
#include <iostream>
int main(int argc, char **args) {
if (argc==0) {
std::cerr << "Error: Can't find executable!" << std::endl;
return 1;
}
std::cerr << "The executable is located at: " << args[0] << std::endl;
return 0;
}
If you run it from a different directory than the executable is located, then that will be part of "args[0]".
Bookmarks