[英]How to get the $(ProjectDir) path in a Visual Studio native C++ Unit Testing using CppUnitTestFramework?
I need to be able to retrieve the project directory path of my unit testing in order to load some files required by the tests. 我需要能够检索单元测试的项目目录路径,以便加载测试所需的一些文件。 I don't want to hard-code it in case the solution structure and absolute paths changes in the future.
如果解决方案结构和绝对路径将来发生变化,我不想对其进行硬编码。
Ok, this is how I did it: 好的,我就是这样做的:
In my project Properties -> Configuration -> C/C++ -> Preprocessor I added this preprocessor definition UNITTESTPRJ="$(ProjectDir)."
在我的项目属性 - >配置 - > C / C ++ - >预处理器中,我添加了这个预处理器定义
UNITTESTPRJ="$(ProjectDir)."
Then in my cpp file I did: 然后在我的cpp文件中我做了:
#define STRINGIFY(x) #x
#define EXPAND(x) STRINGIFY(x)
string s = EXPAND(UNITTESTPRJ);
s.erase(0, 1); // erase the first quote
s.erase(s.size() - 2); // erase the last quote and the dot
string my_project_dir = s;
The stupid .
愚蠢的
.
at the end was necessary to escape the trailing \\"
in the project directory. 最后有必要逃避项目目录中的尾随
\\"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.