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

19
我遇到了标题所述的错误。我已确保以下内容:
- Include目录、include库和额外的include目录都设置正确
- 在属性中,子系统设置为CONSOLE

我的代码注释如下: LifeLib是一个包含我想要测试一些方法的类的项目。这些类在命名空间LifeLib中定义。其中之一是StornoTafel。testVariables没有在任何命名空间中定义。
我在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>

详细错误信息:

  1. LNK2019 未解析的外部符号 "public: __cdecl LifeLib::StornoTafel::StornoTafel(void)" (??0StornoTafel@LifeLib@@QEAA@XZ),在函数 "public: __cdecl AggSelTester::AggSelTest::AggSelTest(void)" (??0AggSelTest@AggSelTester@@QEAA@XZ) 中引用
  2. LNK2019 未解析的外部符号 "public: __cdecl LifeLib::StornoTafel::StornoTafel(class std::vector >)" (??0StornoTafel@LifeLib@@QEAA@V?$vector@NV?$allocator@N@std@@@std@@@Z),在函数 "public: void __cdecl AggSelTester::AggSelTest::init(void)" (?init@AggSelTest@AggSelTester@@QEAAXXZ) 中引用
  3. LNK2019 未解析的外部符号 "public: class LifeLib::StornoTafel __cdecl LifeLib::StornoTafel::operator=(class LifeLib::StornoTafel const &)" (??4StornoTafel@LifeLib@@QEAA?AV01@AEBV01@@Z),在函数 "public: void __cdecl AggSelTester::AggSelTest::init(void)" (?init@AggSelTest@AggSelTester@@QEAAXXZ) 中引用

为什么会出现这些错误?

5个回答

16

我自己找到了答案:我需要包含 .cpp 文件。
所以 #include "../LifeLib/StornoTafel.cpp" 修复了错误。不过我不知道为什么。


11

我知道这很晚了,但让我解释一下为什么包含.cpp文件可以解决这个问题。

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

...

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

你的单元测试项目存在于应用程序之外,你已经提供了对头文件的引用,这些头文件提供了类型和方法的声明,但并没有提供它们的定义

通过引用.cpp文件,编译器可以获取这些签名的声明:

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

...

LifeLib::StornoTafel stornoTafel_; // Works perfectly

最简单的解决方法是简单地复制您的头文件引用并将 .h 更改为 .cpp。


4
这不可能是一个真正的解决方案... 这需要包含很多cpp文件。 - Vegeta
这并没有解释为什么这只会在x64配置下发生。 - user1633272

3

1
问题是名称混淆,因为你从C++调用C。 解决方案是将对C代码头文件的调用包装在Extern C中。
extern "C" {
  #include "../LifeLib/StornoTafel.h"
  #include "../LifeLib/testVariables.h"
}

1
根据我的经验,导致这个错误出现的原因有两个:一是在你使用的软件中,你没有正确地将其包含在你的项目中,因此会出现链接错误;二是你的项目版本和你试图包含的版本之间存在混合。我的意思是,检查它们是否都是64位或32位。如果它们不同,就会出现这个错误。这些是我知道可能会导致这个错误的原因,也可能是其他原因。

一切都运行在64位,所以不应该是这个问题。正如我所说,我确定我输入了正确的包含路径等信息。 - Steve

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接