我正在尝试测试我的简单类来构建(它有很好的构造函数)。
档案TestClass.h
#pragma once
class TestClass
{
public:
TestClass();
};
档案TestClass.cpp
#include "TestClass.h"
TestClass::TestClass()
{
}
然后我在我的解决方案中添加新项目:tests
。该项目只包含一个文件。
档案test.cpp
#include <gtest/gtest.h>
#include "../Example/TestClass.h"
TEST(Test0, all)
{
EXPECT_EQ(true, true);
}
/*
TEST(Test1, part1)
{
TestClass t;
}*/
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
如果我构建该代码 - 一切都很好(以及所有测试)。但如果我取消注释注释块 - 我有这样的输出错误:
1> ------重建全部启动:项目:示例,配置:调试Win32 ------ 1> TestClass.cpp 1> Example.vcxproj - > d:\ Alex \ documents \ visual studio 2015 \ Projects \ tests \ Debug \ Example.lib 2> ------重建全部启动:项目:测试,配置:调试Win32 ------ 2> test.cpp 2> test.obj:错误LNK2019:未解析的外部符号“public:__thiscall TestClass :: TestClass(void)”(?? 0TestClass @@ QAE @ XZ)在函数“private:virtual void __thiscall Test1_part1_Test :: TestBody(void)”中引用“(?TestBody @ Test1_part1_Test @@ EAEXXZ) 2> d:\ Alex \ documents \ visual studio 2015 \ Projects \ tests \ Debug \ tests.exe:致命错误LNK1120:1个未解析的外部 ==========重建全部:1成功,1失败,0跳过==========
我将gtest.lib
文件添加到tests
项目的链接器输入的依赖项中。
我添加了Example
项目作为tests
项目的依赖项。
我将Example
项目构建为静态库(.lib)。
如果我从TestClass
删除构造函数 - 一切都会好的。
我的项目你可以找到here。
我在Windows 10 Pro x64上使用Microsoft Visual Studio 2015和Update 3。
我的Google Test Framework版本是1.8.0。
我该如何解决这个编译问题?我真的需要这个构造函数。
我可以解决这个问题:
我手动添加buildDir
(对我来说是$(SolutionDir)$(Configuration)\
):
tests
属性 - >配置属性 - >链接器 - >常规 - >其他库目录
我手动添加Example.lib
依赖关系:
tests
属性 - >配置属性 - >链接器 - >输入 - >附加依赖项
这正是我现在所遇到的问题。解决方案如下:
希望能帮助到你。:)