Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 432
Solti Avatar asked Aug 15 '14 18:08

Solti


People also ask

How do I fix unresolved external symbol in lnk2019?

A symbol is declared but not defined You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

How do I fix unresolved external symbol?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

How do I use Google test in Visual Studio?

Add a Google Test project in Visual Studio 2022In Solution Explorer, right-click on the solution node and choose Add > New Project. Set Language to C++ and type test in the search box. From the results list, choose Google Test Project. Give the test project a name and choose OK.

What does unresolved external symbol mean?

Unresolved external references occur when the symbol for a function or global variable is referenced in a program, but none of the object files or libraries specified in the link step contain a definition for that symbol.


2 Answers

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;
    }
}
like image 53
tak333 Avatar answered Sep 18 '22 06:09

tak333


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.

like image 32
Solti Avatar answered Sep 22 '22 06:09

Solti