Google Test: error LNK2019: unresolved external symbol with Visual Studio 2013
Asked Answered
O

5

12

I'm trying to get my first ever unit test with Google Test framework + Visual Studio 2013.However I'm hitting the below error and can't understand why.

1>------ Build started: Project: FirstGoogleTest, Configuration: Debug Win32 ------
2>------ Build started: Project: googleTest, Configuration: Debug Win32 ------
1> MyMultiplier.cpp
2> gtest_main.cc
1> main.cpp
1> Generating Code...
2> gtest-all.cc
1> FirstGoogleTest.vcxproj -> D:_Vault\Workspaces\UnitTestLearning\FirstGoogleTest\Debug\FirstGoogleTest.exe
2> Generating Code...
2> googleTest.vcxproj -> D:_Vault\Workspaces\UnitTestLearning\FirstGoogleTest\Debug\googleTest.lib
3>------ Build started: Project: MyMultiplier_UnitLevelTest, Configuration: Debug Win32 ------
3> MyMultiplier_UnitLevelTest.cpp
3>MyMultiplier_UnitLevelTest.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall
MyMultiplier::multiply(unsigned int,unsigned int)" (?multiply@MyMultiplier@@QAEIII@Z) referenced in function "private: virtual void __thiscall MyMultiplier_multiplyNormalSmallValues_Test::TestBody(void)" (?TestBody@MyMultiplier_multiplyNormalSmallValues_Test@@EAEXXZ)
3>D:_Vault\Workspaces\UnitTestLearning\FirstGoogleTest\Debug\MyMultiplier_UnitLevelTest.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 2 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The solution structures is as follow:
https://www.dropbox.com/s/0cu8eqr7pz3ajaz/Untitled.png

The ULT project's "References" projects include "googleTest" and "FirstGoogleTest". here is the "MyMultiplier_UnitLevelTest.cpp"

#include "gtest/gtest.h"  // access test macro
#include "MyMultiplier.h"   // testee
#include <iostream>
#include <string>

TEST(MyMultiplier, multiplyNormalSmallValues){
    MyMultiplier m;
    std::string name("MyMultiplier_ULT");
    unsigned int a = 5;
    unsigned int b = 10;
    unsigned int answer = m.multiply(a, b/*, name*/);
    ASSERT_EQ(a * b, answer);

}

the "multiply" function's declaration in MyMultiplier.h:

class MyMultiplier{
public:
    unsigned int multiply(unsigned int a, unsigned int b/*, std::string& name*/);
};

the signature matches and also the header file is included. why the ult project can't find the symbol?

the entire solution can be downloaded here: https://www.dropbox.com/sh/vc89o5ep139wkuk/AAA8Z76q6iAkP25zTmu9bR3ia

Orangy answered 15/8, 2014 at 18:43 Comment(2)
are you linking correctly between your projects? i.e. you should link against the .lib's in your .vcxprojBlaeberry
is this a manual step? I didn't manually link the projects and kind of assume that's something Visual studio would do. I did add references of the ULT project to other projects.Orangy
O
18

The root cause is the project type is not set correctly.

In this example, there are three projects:

  1. FirstGoogleTest, which is the testee. the class to be tested resides in here.
  2. googleTest, which is the google test framework
  3. MyMultiplier_UnitLevelTest, which is the ULT project that contains the tests.

The root cause is "FirstGoogleTest" project's configuration Type was set to .exe, which is the same as the ULT project. so the ult test cannot get the externals from "FirstGoogleTest". After changing "FirstGoogleTest" configuration Type to Static library (.lib). the solution can be compiled correctly and the ULT runs fine.

Orangy answered 15/8, 2014 at 23:0 Comment(2)
This is not a good solution. It requires everything you test to be a library. What if you are testing an executable? Are you going to maintain a second set of project files to build everything into libraries just for unit testing?Jitter
@Jitter I'm a bit late to the party, but you can link against the .obj files generated during the exe compilation process without having to swap from an exe to a lib project compilation. this is what I did when facing this issue because your comment is correct, having a different build configuration that spits out a library when you need to test anything is annoying.Duggins
J
2

The issue here is that you didn't include MyMultiplier.h/cpp in the actual test project.

Add it to the project file (right-click, add existing item and navigate to the files).

Jitter answered 20/3, 2017 at 14:32 Comment(0)
T
2

I solved a similar problem by including the cpp files (I created a Visual Studio Project for testing with GoogleTest my own class "Number" (.h + .cpp))

Didn't work:

#include "pch.h"
#include "..\..\LAB2\Number.h"

class TestLab2 : public ::testing::Test {
public:
    TestLab2() {
        Number n;
    }
}

Worked:

#include "pch.h"
#include "..\..\LAB2\Number.h"
#include "..\..\LAB2\Number.cpp"

class TestLab2 : public ::testing::Test {
public:
    TestLab2() {
        Number n;
    }
}
Turbine answered 16/6, 2021 at 22:40 Comment(2)
You should never include the .cpp files with definitions. If you include it in more than one place in your project you will have redefinitions and thus linker error.Centrist
I know, but somehow only this way works.Turbine
C
1

Adding dependencies for .lib files is a manual step in Visual Studio.

  1. Open the Property Pages box for your project by right clicking on your project in the solution explorer (in your case, MyMultiplier_UnitLevelTest)
  2. Click on the Linker folder
  3. Open the Input page
  4. Add any necessary .libs in the Additional Dependencies field

More information can be found here: http://msdn.microsoft.com/en-CA/library/ba1z7822.aspx

Carmencita answered 15/8, 2014 at 20:3 Comment(5)
in this case, what would be the missing lib file? the error is referring to multiply function inside MyMultiplier. but MyMultiplier is not a lib file...Orangy
How are the functions in FirstGoogleTest and googleTest exported for your use in MyMultiplier_UnitLevelTest? Perhaps I've misunderstood your comment response - but I assumed that you were using .dlls and had already generated .lib files.Carmencita
I followed a Google Test tutorial video, in which the ULT project only need to do (1) add FirstGoogleTest and GoogleTest as "Reference" projects to the ULT project (2) add the directory paths of FirstGoogleTest and Google test to ULT project's "Include Directories" property<br> in the tutorial video, it works totally fine but not on my case.Orangy
It turns out your direction is right. it's about the library linking stuff. I need to change the testee project type to static library to resolve the issue.Orangy
Ah I see - didn't realize it could all be done in one step. Glad that it's working for you.Carmencita
P
-1

I had the same problem, and the easiest solution when you want a standalone google test is to link gtest_main as well as gtest. You can set your Visual Studio project as Executable and then link with gtest_main and gtest.

Philander answered 3/1, 2018 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.