visual studio 2013 error LNK1120: 1 unresolved externals and error LNK2019: unresolved external symbol

visual studio 2013 error LNK1120: 1 unresolved externals and error LNK2019: unresolved external symbol

我是 google 测试的新手 framework.This 是我的第一个 google 测试 project.I 做了像这个网站这样的配置 http://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php 但是我更改了 way.Now 上的 .cpp 文件,出现了上述链接错误。

我的输出如下

1>------ 全部重建开始:项目:GoogleTest,配置:调试 Win32 ------ 2>------ 全部重建开始:项目:SimpleMath,配置:调试Win32 ------ 1> gtest_main.cc 2> SimpleMath.cpp 2> SimpleMath.vcxproj -> D:\My Document\cpp\GoogleTest\Debug\SimpleMath.exe 1> gtest-all.cc 1> 正在生成代码... 1>LINK:警告 LNK4068:/MACHINE 未指定;默认为 X86 1> GoogleTest.vcxproj -> D:\My Document\cpp\GoogleTest\Debug\GoogleTest.lib 3>------ 全部开始重建:项目:unittest_cube,配置:调试 Win32 --- --- 3> unittest_cube.cpp 3> stdafx.cpp 3> 正在生成代码... 3>unittest_cube.obj:错误 LNK2019:未解析的外部符号 "public: double __thiscall Cube::cubic(double)" (?cubic@Cube@@ QAENN@Z) 在函数中引用 "private: virtual void __thiscall testMath_myCubeTest_Test::TestBody(void)" (?TestBody@testMath_myCubeTest_Test@@EAEXXZ) 3>D:\My Document\cpp\GoogleTest\Debug\unittest_cube.exe : fatal error LNK1120: 1 unresolved externals ========== 全部重建:2 次成功,1 次失败,0 次跳过 ==========

如何修复此链接错误。提前致谢。

我的unittest_cube.cpp文件(在测试项目里面)如下

#include "gtest/gtest.h"
#include "simplemath.h"


int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

TEST(testMath, myCubeTest)
{   
    Cube c;
    EXPECT_EQ(1000.0, c.cubic(10));
    //EXPECT_TRUE(c.cubic(2) == 8);
    //ASSERT_EQ(1000.0, c.cubic(10));
}`

我的SimpleMath.cpp就像

#include "simplemath.h"
#include <iostream>

using namespace std;
int main()
{
    Cube c;
    double num= c.cubic(10);
    //cout << num;
    //getchar();
    return 0;
}

double Cube::cubic(double d)
{
    return pow(d, 3);
}

我的 simplemath.h 文件就像

#include <cmath>


class Cube{
public:
    double cubic(double i);
};

这里有两个问题:

  1. 第一个是您确实向 VS 项目添加了 SimpleMath.cpp 测试(您的输出表明您正在使用 Visual Studio)。为此,请在解决方案资源管理器中右键单击项目的 "Source Files" 过滤器,select "Existing Item ...",然后添加文件 SimpleMath.cpp。这应该可以解决第一个问题。
  2. 第二个问题是您有两个 main 函数。这意味着在正确添加文件 SimpleMath.cpp 后,您将收到另一个链接器错误。删除或注释掉 SimpleMath.cpp 中的主要函数以成功构建您的代码。