【发布时间】:2012-12-23 11:34:48
【问题描述】:
我想为 Visual C++ 项目创建单元测试。我尝试关注these MSDN instructions。我找到了区分非托管/混合/纯代码的页面,但我并不完全理解这些概念。我的代码不使用 .NET,可能会在 MinGW 下通过一些代码调整进行编译。
我的主项目构建了一个可执行文件,因此我按照从测试项目中引用导出的函数中的步骤进行操作。对于初学者,我有不同的项目选择:
我选择了 Native Unit Test Project。我添加了对我的主项目的引用,并将 Include Directories 设置为 $(SolutionDir)\Cubes;$(IncludePath)。我编写了我的代码并在编译时得到了这个:
1>Creating library C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.lib and object C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.exp
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: __thiscall Room::Room(void)" (??0Room@@QAE@XZ) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency@LayoutTest@CubesTest@@QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: __thiscall Layout::Layout(class Room *,int)" (??0Layout@@QAE@PAVRoom@@H@Z) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency@LayoutTest@CubesTest@@QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Layout::add(int,int,class Room *)" (?add@Layout@@QAEXHHPAVRoom@@@Z) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency@LayoutTest@CubesTest@@QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Layout::clear(int,int,bool)" (?clear@Layout@@QAEXHH_N@Z) referenced in function __catch$?NumOfRoomsConsistency@LayoutTest@CubesTest@@QAEXXZ$0
1>C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.dll : fatal error LNK1120: 4 unresolved externals
如果我没记错的话,这意味着编译器会找到头文件,而不是源文件。我错过了什么?
【问题讨论】:
-
不,这意味着 linker 找不到包含您引用的符号的二进制文件。
-
对,我如何告诉链接器在哪里可以找到所需的文件?我已经尝试过this,但没有成功。
-
这应该可以解决问题。还要确保你真的编译所有包含你的文件的项目(检查依赖关系)
-
遗憾的是,即使在按顺序清理和重建两个项目之后,我仍然会遇到错误。我还尝试改用
$(SolutionDir)\Cubes\Debug,它似乎存储了.obj文件。我来自MinGW背景,所以我习惯将源代码编译为.o文件。 -
检查链接器->输入->附加依赖。手动添加您的
.lib文件
标签: unit-testing visual-c++ visual-studio-2012