Search code examples
c++visual-studio-2013googletest

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


I am newbe to google test framework.This is my very first google test test project.I did the configurations like this site http://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php but I change the .cpp file on my way.Now the above linking error was appeared.

My output is as follows

1>------ Rebuild All started: Project: GoogleTest, Configuration: Debug Win32 ------ 2>------ Rebuild All started: Project: SimpleMath, Configuration: Debug Win32 ------ 1> gtest_main.cc 2> SimpleMath.cpp 2> SimpleMath.vcxproj -> D:\My Document\cpp\GoogleTest\Debug\SimpleMath.exe 1> gtest-all.cc 1> Generating Code... 1>LINK : warning LNK4068: /MACHINE not specified; defaulting to X86 1> GoogleTest.vcxproj -> D:\My Document\cpp\GoogleTest\Debug\GoogleTest.lib 3>------ Rebuild All started: Project: unittest_cube, Configuration: Debug Win32 ------ 3> unittest_cube.cpp 3> stdafx.cpp 3> Generating Code... 3>unittest_cube.obj : error LNK2019: unresolved external symbol "public: double __thiscall Cube::cubic(double)" (?cubic@Cube@@QAENN@Z) referenced in function "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 ========== Rebuild All: 2 succeeded, 1 failed, 0 skipped ==========

How to fix this linking error. Thanks in advance.

My unittest_cube.cpp file(which inside the test project) as follows

#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));
}`

My SimpleMath.cpp is like

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

My simplemath.h file is like

#include <cmath>


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

Solution

  • You have two issues here:

    1. First one is that you did add SimpleMath.cpp test to your VS project (your output suggests that you are using Visual Studio). To do so, right click on "Source Files" filter of your project in Solution Explorer, select "Existing Item ...", and add file SimpleMath.cpp. This should resolve the first issue.
    2. The second issue is that you have two main functions. This means that you will get another linker error after you properly add file SimpleMath.cpp. Remove or comment out the main function in SimpleMath.cpp to successfully build your code.