我正在尝试测试我的简单类来构建(它有很好的构造函数)
档案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&gt; ------ Rebuild All started:Project:Example,Configuration:Debug Win32 ------
1 GT; TestClass.cpp
1 GT; Example.vcxproj - &gt; d:\ Alex \ documents \ visual studio 2015 \ Projects \ tests \ Debug \ Example.lib
2&gt; ------重建全部启动:项目:测试,配置:调试Win32 ------
2 - ; TEST.CPP
2&gt; 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&gt; 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。
如何解决此编译问题?我真的需要这个构造函数。
答案 0 :(得分:0)
我可以解决这个问题:
我以这种方式手动添加buildDir
(对我来说是$(SolutionDir)$(Configuration)\
):
tests
属性 - &gt;配置属性 - &gt;链接器 - &gt;一般 - &gt;其他图书馆目录
我手动添加Example.lib
依赖关系:
tests
属性 - &gt;配置属性 - &gt;链接器 - &gt;输入 - &gt;其他依赖性
答案 1 :(得分:0)
这正是我现在所遇到的问题。解决方案如下:
确保添加 参考 :在测试项目下,添加对源项目的引用。
将包含 TestClass.cpp 和 TestClass.h 的项目的 配置属性 设置为 静态库 :在VS 2015中,它位于项目 - &gt;下属性 - &gt;配置属性 - &gt;一般 - &gt;项目默认值 - &gt;配置类型。此外,请注释源项目中的主要功能。
希望它有所帮助。:)