简体   繁体   English

LNK2019:VS 单元测试中未解析的外部符号

[英]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 - 在属性中,子系统设置为 CONSOLE

Comments to my code: LifeLib is a project that contains classes of that I want to test some methods.对我的代码的评论: LifeLib 是一个包含我想测试一些方法的类的项目。 The classes are defined in namespace LifeLib.这些类在命名空间 LifeLib 中定义。 One of them is StornoTafel.其中之一是 StornoTafel。 testVariables is NOT defined in any namespace. testVariables 未在任何命名空间中定义。
I get the linking error 3 times, for 2 constructors and 1 method in StornoTafel (noted in the code).对于 StornoTafel 中的 2 个构造函数和 1 个方法(在代码中注明),我收到 3 次链接错误。

//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) LNK2019 未解析的外部符号“public: __cdecl LifeLib::StornoTafel::StornoTafel(void)” (??0StornoTafel@LifeLib@@QEAA@XZ) 在函数“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) LNK2019 未解析的外部符号“public: __cdecl LifeLib::StornoTafel::StornoTafel(class std::vector >)”(??0StornoTafel@LifeLib@@QEAA@V?$vector@NV?$allocator@N@std@@@函数“public: void __cdecl AggSelTester::AggSelTest::init(void)”中引用的 std@@@Z) (?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) LNK2019 未解析的外部符号“public: class LifeLib::StornoTafel __cdecl LifeLib::StornoTafel::operator=(class LifeLib::StornoTafel const &)”(??4StornoTafel@LifeLib@@QEAA?AV01@AEBV01@@Z) 中引用函数“公共:void __cdecl AggSelTester::AggSelTest::init(void)”(?init@AggSelTest@AggSelTester@@QEAAXXZ)

Why do they arise?它们为什么会出现?

I found the answer by myself: I have to include the .cpp file.我自己找到了答案:我必须包含 .cpp 文件。
So #include "../LifeLib/StornoTafel.cpp" fixes the error.所以#include "../LifeLib/StornoTafel.cpp"修复了错误。 However, I have no idea why.但是,我不知道为什么。

I know this is very late but let me explain why including the .cpp file fixes the issue.我知道这已经很晚了,但让我解释一下为什么包含 .cpp 文件可以解决问题。

#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:通过放置对 .cpp 文件的引用,编译器实际上可以获得这些签名的声明:

#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最简单的解决方案是简单地复制您的头文件引用并将 .h 更改为 .cpp

I had similar problem and fixed it by adding the output .obj files to the dependencies of the test project.我有类似的问题,并通过将输出 .obj 文件添加到测试项目的依赖项来修复它。

refer to MSDN document - https://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef请参阅 MSDN 文档 - https://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef

In my experience two things make this error show up, either in the software you are using you didn't properly include it in your project, therefor the link error shows up, or maybe there is a mix between the versions of both your project and the one you try to include.根据我的经验,有两件事会导致此错误出现,要么是在您使用的软件中,您没有将其正确包含在您的项目中,因此出现了链接错误,或者您的项目和版本之间可能存在混合您尝试包含的那个。 By this i mean, check if they are both 64 or 32. if they are not the same this error will show up.我的意思是,检查它们是 64 还是 32。如果它们不相同,则会出现此错误。 These are the things i know can cause this, it can be something else.这些是我所知道的可能导致这种情况的原因,也可能是其他原因。

The issue is name mangling because you are calling from c++ to c.问题是名称修改,因为您是从 c++ 调用 c。 Solution is wrap call to c code header with Extern C解决方案是使用 Extern C 包装对 c 代码头的调用

extern "C" {
  #include "../LifeLib/StornoTafel.h"
  #include "../LifeLib/testVariables.h"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM