In C, when using the FILE
struct from stdio.h
, the current working directory will be relative to the build directory if the programmer attempts to open a file.
Is there a setting of some sort (maybe a compiler flag) which I can use to allow the CWD to be set to the source directory at compile or run time? Preferably a method that is cross-platform from Windows to *nix, but if that's not doable let's just say *nix.
I'm working in Linux currently, with GCC/G++, using qmake without the Qt libraries.
Also, the main reason I'm using the C FILE
IO method as opposed to C++'s std::ifstream
is just personal preference, in case anyone asks.
In your .pro file you can define the source directory as a macro, then chdir()
to it in your source code.
# myproject.pro
DEFINES+=SRCDIR=\\\"$$PWD\\\"
# myapp.cpp
int main(int argc, char** argv) {
if (chdir(SRCDIR)) {
perror("chdir to " SRCDIR);
}
// ... rest of code
}
Getting it to work on Windows might involve fiddling with the number of backslashes in the .pro file.