只有定义在.h文件中时,GoogleTest才有效

时间:2013-11-05 08:58:15

标签: c++ visual-studio visual-studio-2013 googletest

我花了一整天的时间尝试让GoogleTest在Visual Studio 2013中运行。最后,让它“工作”,但只有当函数的定义放在.h文件中时才有效。使用单独的编译,例如=:

// simplemath.h
#include <cmath>
double cubic(double d);

// simple.cpp
#include "simplemath.h"
double cubic(double d)
{
    return pow(d, 3);
}

// unittest_SimpleMath.cpp
#include "gtest/gtest.h"
#include "simplemath.h"    
TEST(testMath, myCubeTest)
{
    EXPECT_EQ(1000, cubic(10));
}  

产生以下错误:

1>------ Build started: Project: unittest_SimpleMath, Configuration: Release Win32 ------
1>  unittest_SimpleMath.cpp
1>unittest_SimpleMath.obj : error LNK2001: unresolved external symbol "double __cdecl cubic(double)" (?cubic@@YANN@Z)
1>C:\Users\alex\Documents\Visual Studio 2013\Projects\SimpleMath\Release\unittest_SimpleMath.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

编辑:忘了提一件重要的事情。我按照教程http://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php来弄清楚如何配对Gtest和VS2013。我的解决方案结构与desc相同。在教程中 - 3个项目。

1 个答案:

答案 0 :(得分:1)

您似乎正在使用不同的项目进行测试。您应该在具有cubic功能的同一项目中使用测试。或者从您的cubic代码中创建lib并将其链接到测试项目中。您遇到的错误没有以任何方式连接到gtest。您还没有将cpp文件编译为可以在测试项目中使用的目标文件。

此外,请确保您的测试项目中有gtest_main*.lib(星号,因为它有一些名称,您应该选择您需要的任何内容),因为您没有main(或者你没有表现出来。)