我正试图通过Google Test框架+ Visual Studio 2013进行我的第一次单元测试。但是我遇到了以下错误并且无法理解原因。
1> ------ Build build:Project:FirstGoogleTest,Configuration:Debug Win32 ------ 2> ------ Build build:项目:googleTest,配置:调试Win32 ------ 1> MyMultiplier.cpp 2> gtest_main.cc 1> main.cpp 1>生成代码...... 2> gtest-all.cc 1> FirstGoogleTest.vcxproj - > D:_Vault \ Workspaces \ UnitTestLearning \ FirstGoogleTest \ Debug \ FirstGoogleTest.exe 2>生成代码...... 2> googleTest.vcxproj - > D:_Vault \ Workspaces \ UnitTestLearning \ FirstGoogleTest \ Debug \ googleTest.lib 3> ------ Build build:Project:MyMultiplier_UnitLevelTest,Configuration:Debug Win32 ------ 3> MyMultiplier_UnitLevelTest.cpp 3> MyMultiplier_UnitLevelTest.obj:错误LNK2019:未解析的外部符号“public:unsigned int __thiscall MyMultiplier :: multiply(unsigned int,unsigned int)“(?multiply @ MyMultiplier @@ QAEIII @ Z)在函数”private:virtual void __thiscall MyMultiplier_multiplyNormalSmallValues_Test :: TestBody(void)“中引用(?TestBody @MyMultiplier_multiplyNormalSmallValues_Test @@ EAEXXZ) 3> D:_Vault \ Workspaces \ UnitTestLearning \ FirstGoogleTest \ Debug \ MyMultiplier_UnitLevelTest.exe:致命错误LNK1120:1未解析的外部 ==========构建:2成功,1失败,0最新,0跳过==========
解决方案结构如下: https://www.dropbox.com/s/0cu8eqr7pz3ajaz/Untitled.png
ULT项目的“参考”项目包括“googleTest”和“FirstGoogleTest”。这是“MyMultiplier_UnitLevelTest.cpp”
#include "gtest/gtest.h" // access test macro
#include "MyMultiplier.h" // testee
#include <iostream>
#include <string>
TEST(MyMultiplier, multiplyNormalSmallValues){
MyMultiplier m;
std::string name("MyMultiplier_ULT");
unsigned int a = 5;
unsigned int b = 10;
unsigned int answer = m.multiply(a, b/*, name*/);
ASSERT_EQ(a * b, answer);
}
My Multiplier.h中的“multiply”函数声明:
class MyMultiplier{
public:
unsigned int multiply(unsigned int a, unsigned int b/*, std::string& name*/);
};
签名匹配,还包括头文件。为什么ult项目找不到符号?
整个解决方案可以在这里下载:https://www.dropbox.com/sh/vc89o5ep139wkuk/AAA8Z76q6iAkP25zTmu9bR3ia
根本原因是项目类型未正确设置。
在此示例中,有三个项目:
根本原因是“FirstGoogleTest”项目的配置类型设置为.exe,这与ULT项目相同。所以ult测试不能从“FirstGoogleTest”获得外部。将“FirstGoogleTest”配置更改为静态库(.lib)后。解决方案可以正确编译,ULT运行正常。
这里的问题是你没有在实际的测试项目中包含MyMultiplier.h / cpp。
将其添加到项目文件中(右键单击,添加现有项目并导航到文件)。
为.lib文件添加依赖项是Visual Studio中的手动步骤。
更多信息可以在这里找到:http://msdn.microsoft.com/en-CA/library/ba1z7822.aspx
我有同样的问题,当你想要一个独立的谷歌测试时,最简单的解决方案是链接gtest_main和gtest。您可以将Visual Studio项目设置为可执行文件,然后使用gtest_main和gtest进行链接。