1

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);
};
  • 1
    Doesn't look like your actual cpp file (Cube.cpp?) is included in the compilation...? – Joachim Isaksson Feb 02 '16 at 05:06
  • Try making sure that all of your `.cpp` and/or `.cc` files are being passed to the linker, that they all have the names it expects them to have, and that they're all where it expects them to be? The issue is probably that it can't find one of them. See here: http://stackoverflow.com/questions/14289170/lnk1120-1-unresolved-externalsand-lnk2019-unresolved-external-symbol?rq=1 ...Specifically, I believe it's the file containing `Cube::cubic(double)`, so make sure it's compiling & linking `SimpleMath.cpp`, too. – Justin Time - Reinstate Monica Feb 02 '16 at 05:40
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Niall Feb 02 '16 at 06:28
  • Thanks All for solutions. – Samitha Palihawadana Feb 02 '16 at 09:07

1 Answers1

3

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.
Marko Popovic
  • 3,999
  • 3
  • 22
  • 37