简体   繁体   English

GoogleTest仅在.h文件中定义时才起作用

[英]GoogleTest only work when definition is in .h file

I have spend all day trying to make GoogleTest work in Visual Studio 2013. Finally, made it to "work", but it only works when definition of the function is place in the .h file. 我已经花了整整一天的时间尝试使GoogleTest在Visual Studio 2013中工作。最后,使其成为“工作”状态,但是仅当函数定义位于.h文件中时,它才起作用。 Using separate compilation, such as =: 使用单独的编译,例如=:

// 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));
}  

produces the following error: 产生以下错误:

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 ==========

Edit: Forgot to mention one important thing. 编辑:忘记提及一件重要的事。 I followed tutorial http://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php to figure out how to pair-up Gtest and VS2013. 我遵循了http://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php教程,以了解如何配对Gtest和VS2013。 My solution structure is identical to what is desc. 我的解决方案结构与desc相同。 in the tutorial - 3 projects. 在本教程中-3个项目。

It seems you are using different project for the tests. 看来您正在使用其他项目进行测试。 You should either use tests in the same project you have your cubic function. 您应该在具有cubic函数的同一项目中使用测试。 Or make lib from your cubic code and link it in the tests project. 或者从您的cubic代码中创建lib并将其链接到测试项目中。 The errors you have aren't connected to gtest in any way. 您所犯的错误与gtest无关。 You just have not compiled your cpp file to an object file which could be used in the tests project. 您只是没有将cpp文件编译为可在测试项目中使用的目标文件。

Also, make sure you have gtest_main*.lib (asterisk there because it has a few name, you should choose whatever you need) linked in your test project since you don't have main for it(or you didn't show it). 另外,请确保在测试项目中链接了gtest_main*.lib (星号,因为它有几个名称,您应该选择所需的名称),因为您没有它的main (或者您没有显示它) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM