Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LNK2019: unresolved external symbol in VS unit-testing

I get the error as stated in the title. I ensured the following:
- The Include directory, include library and additional include directory are set correctly
- In the properties, Subsystem is set to CONSOLE

Comments to my code: LifeLib is a project that contains classes of that I want to test some methods. The classes are defined in namespace LifeLib. One of them is StornoTafel. testVariables is NOT defined in any namespace.
I get the linking error 3 times, for 2 constructors and 1 method in StornoTafel (noted in the code).

//project Tester
#include "stdafx.h"
#include "CppUnitTest.h"

#include "../LifeLib/StornoTafel.h"
#include "../LifeLib/testVariables.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace Tester
{       
    TEST_CLASS(AggSelTest)
    {
    public:
        LifeLib::StornoTafel stornoTafel_; // LNK2019
        LifeLib::StornoTafel *stornoTafel_; // no error, but I need an instance and not a reference to proceed -> see init method
        LifeLib::testVariables test_vars_; // everything is fine

        TEST_METHOD_INITIALIZE(init) {
            stornoTafel_ = StornoTafel(test_vars_.lapseProb); // when this line is commented out I only get the first error (see below)
        }
    }
}

// testVariables.h
#pragma once
#include <iostream>
#include <vector>

class testVariables {
public:
    testVariables() {};
// here are a lot of vectors with values for testing purposes
std::vector<double> _lapseProb= {0,1,2}; // [...]
};

// StornoTafel.h
#pragma once
#include "masterheader.h"

namespace LifeLib {
    class StornoTafel {
    public:

        StornoTafel(); //LNK2019
        StornoTafel(std::vector<double> ProbabilityOfLapseInYearT); //LNK2019

        StornoTafel(const StornoTafel &obj); //no error

        StornoTafel operator=(StornoTafel const& rhs); //LNK2019

        //! \name Getter
        //@{ 
        const std::vector<double>& Stornowahrscheinlichkeit() const;
        //@}
    protected:
        std::vector<double> Stornowahrscheinlichkeit_;
    };
    inline const std::vector<double>& StornoTafel::Stornowahrscheinlichkeit() const {
        return Stornowahrscheinlichkeit_;
    }
}

//StornoTafel.cpp
#include "StornoTafel.h"

LifeLib::StornoTafel::StornoTafel() {
}

LifeLib::StornoTafel::StornoTafel(std::vector<double> ProbabilityOfLapseInYearT) {
    Stornowahrscheinlichkeit_ = ProbabilityOfLapseInYearT;
}

LifeLib::StornoTafel::StornoTafel(const StornoTafel &obj) {
    Stornowahrscheinlichkeit_ = obj.Stornowahrscheinlichkeit_;
}

LifeLib::StornoTafel LifeLib::StornoTafel::operator=(StornoTafel const& rhs) {
    Stornowahrscheinlichkeit_ = rhs.Stornowahrscheinlichkeit_;
    return *this;
}

//masterheader.h
#pragma once
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

#include <algorithm>
#include <ctime>

errors in detail:

  1. LNK2019 unresolved external symbol "public: __cdecl LifeLib::StornoTafel::StornoTafel(void)" (??0StornoTafel@LifeLib@@QEAA@XZ) referenced in function "public: __cdecl AggSelTester::AggSelTest::AggSelTest(void)" (??0AggSelTest@AggSelTester@@QEAA@XZ)
  2. LNK2019 unresolved external symbol "public: __cdecl LifeLib::StornoTafel::StornoTafel(class std::vector >)" (??0StornoTafel@LifeLib@@QEAA@V?$vector@NV?$allocator@N@std@@@std@@@Z) referenced in function "public: void __cdecl AggSelTester::AggSelTest::init(void)" (?init@AggSelTest@AggSelTester@@QEAAXXZ)
  3. LNK2019 unresolved external symbol "public: class LifeLib::StornoTafel __cdecl LifeLib::StornoTafel::operator=(class LifeLib::StornoTafel const &)" (??4StornoTafel@LifeLib@@QEAA?AV01@AEBV01@@Z) referenced in function "public: void __cdecl AggSelTester::AggSelTest::init(void)" (?init@AggSelTest@AggSelTester@@QEAAXXZ)

Why do they arise?

like image 770
Steve Avatar asked Nov 27 '17 16:11

Steve


People also ask

How do I fix unresolved external symbol in Visual Studio?

If the error message is similar to unresolved external symbol WinMain referenced in function function_name, link by using /SUBSYSTEM:CONSOLE instead of /SUBSYSTEM:WINDOWS . For more information about this setting, and for instructions on how to set this property in Visual Studio, see /SUBSYSTEM (Specify Subsystem).

How do I fix lnk2001 unresolved external symbol?

To fix this issue, add the /NOENTRY option to the link command. This error can occur if you use incorrect /SUBSYSTEM or /ENTRY settings in your project. For example, if you write a console application and specify /SUBSYSTEM:WINDOWS, an unresolved external error is generated for WinMain .

What is unresolved external symbol?

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 found the answer by myself: I have to include the .cpp file.
So #include "../LifeLib/StornoTafel.cpp" fixes the error. However, I have no idea why.

like image 107
Steve Avatar answered Oct 18 '22 10:10

Steve


I know this is very late but let me explain why including the .cpp file fixes the issue.

#include "../LifeLib/StornoTafel.h"

...

LifeLib::StornoTafel stornoTafel_; // Throws unresolved external symbol

Your unit test project exists outside of your application, you have provided references to the header files which provide the declarations of your types and methods but not their definitions.

By placing references to the .cpp files the compiler can actually get the declarations of these signatures:

#include "../LifeLib/StornoTafel.h"
#include "../LifeLib/StornoTafel.cpp"

...

LifeLib::StornoTafel stornoTafel_; // Works perfectly

The simplest solution is to simply duplicate your header file reference and change .h to .cpp

like image 40
Liam Avatar answered Oct 18 '22 10:10

Liam