There is generally no anything.cpp
in the target environment (unless you put it there explicitly but the fact remains, it's not the source file that's running), all you have is executables.
There are ways to get the path of the executable so, if it's in a bin
subdirectory, you can still go relative from that. But that's usually a bad idea in multi-user environments since they usually share the executable in a single location.
That's why Windows worked so hard to give us things like c:\users
and UNIX has $HOME
(along with the bin/var/etc
areas to ease software control).
The code as you have it will work relative to your current directory, which will work well provided it's set correctly. So, if you have:
toplevel
|
+--- bin
|
+--- files
and you're running in bin
, you will get the correct file. However, I'd still maintain it's not necessarily such a good idea.
There's plenty of other ways of doing it, such as providing full path on the command line or in an environment variable. But, if you want full automatic directory selection, it'll have to be from information you can get, such as current directory, user's home directory, executable path or so on.
If you can guarantee that the location won't change in the target environment, there are also ways to get the path at source time (during compilation).
The first step to try is using the __FILE__
macro which gives you the presumed source file name. If this contains the path (not mandated), you could use that to work out the directory it's in. You can test if it contains the path with something like:
#include <iostream>
int main (void) {
std::cout << __FILE__ << '\n';
return 0;
}
If you don't get the full path using that method, there's nothing stopping you from providing it yourself, such as changing your compilation command to something like:
gcc -DSRCDIR1=\"$(dirname $PWD/qq.c)\" -DSRCDIR2=\"$(dirname qq.c)\" -o qq qq.c
(though qq.c
would be something like $<
in a makefile
rule).
You could then use code such as:
#include <iostream>
int main (void) {
std::cout << ((SRCDIR2[0] != '/') ? SRCDIR1 : SRCDIR2) << '\n';
return 0;
}
to select the right directory (absolute or relative-turned-into-absolute).