且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 CppUnitTestFramework 在 Visual Studio 原生 C++ 单元测试中获取 $(ProjectDir) 路径?

更新时间:2023-11-08 12:40:52

好的,我是这样做的:

  1. 在我的项目属性 -> 配置 -> C/C++ -> 预处理器中,我添加了这个预处理器定义 UNITTESTPRJ="$(ProjectDir)."

然后在我的 cpp 文件中我做了:

Then in my cpp file I did:

#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.