[英]Set CWD to Source Directory, as opposed to Build Directory
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. 在C语言中,当使用stdio.h
的FILE
结构时,如果程序员尝试打开文件,则当前工作目录将相对于build目录。
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? 是否有某种设置(也许是编译器标志)可用于在编译或运行时将CWD设置为源目录? Preferably a method that is cross-platform from Windows to *nix, but if that's not doable let's just say *nix. 最好是从Windows到* nix跨平台的方法,但是如果这样做不可行,我们就说* nix。
I'm working in Linux currently, with GCC/G++, using qmake without the Qt libraries. 我目前正在使用GCC / G ++在Linux中使用不带Qt库的qmake。
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. 另外,我使用C FILE
IO方法而不是C ++的std::ifstream
主要原因仅仅是个人喜好,以防万一。
In your .pro file you can define the source directory as a macro, then chdir()
to it in your source code. 在.pro文件中,您可以将源目录定义为宏,然后在源代码中将其定义为chdir()
。
# 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. 要使其在Windows上运行,可能需要弄弄.pro文件中的反斜杠数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.