【问题标题】:LNK2019: unresolved external symbol in VS unit-testingLNK2019:VS 单元测试中未解析的外部符号
【发布时间】:2018-05-10 22:29:22
【问题描述】:

我收到标题中所述的错误。我确保了以下几点:
- 包含目录、包含库和附加包含目录设置正确
- 在属性中,Subsystem 设置为 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 未解决的外部 符号“公共:__cdecl LifeLib::StornoTafel::StornoTafel(类 标准::向量>)" (??0StornoTafel@LifeLib@@QEAA@V?$vector@NV?$allocator@N@std@@@std@@@Z) 在函数“public: void __cdecl”中引用 AggSelTester::AggSelTest::init(void)" (?init@AggSelTest@AggSelTester@@QEAAXXZ)
  3. LNK2019 未解决的外部 符号“公共:类 LifeLib::StornoTafel __cdecl LifeLib::StornoTafel::operator=(类 LifeLib::StornoTafel const &)" (??4StornoTafel@LifeLib@@QEAA?AV01@AEBV01@@Z) 在函数中引用 “公共:无效__cdecl AggSelTester::AggSelTest::init(void)” (?init@AggSelTest@AggSelTester@@QEAAXXZ)

它们为什么会出现?

【问题讨论】:

    标签: c++ visual-studio unit-testing linker-errors lnk2019


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我知道这已经很晚了,但让我解释一下为什么包含 .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

      【讨论】:

      • 这不可能是一个实际的解决方案...这需要包含很多 cpp 文件
      • 这并不能解释为什么这只发生在 x64 配置中。
      【解决方案3】:

      根据我的经验,有两件事会导致此错误出现,要么是在您使用的软件中,您没有将其正确包含在您的项目中,因此出现了链接错误,或者两者的版本之间可能存在混合您的项目和您尝试包含的项目。我的意思是,检查它们是 64 还是 32。如果它们不相同,则会出现此错误。我知道这些是可能导致这种情况的原因,也可能是其他原因。

      【讨论】:

      • 一切都以 64 位运行,所以不应该这样。正如我所说,我确信我输入了正确的包含路径等。
      【解决方案4】:

      我遇到了类似的问题,并通过将输出 .obj 文件添加到测试项目的依赖项来解决它。

      参考 MSDN 文档 - https://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef

      【讨论】:

        猜你喜欢
        • 2014-11-07
        • 2013-06-01
        • 1970-01-01
        • 2020-10-31
        • 2022-01-06
        • 2014-09-19
        • 2012-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多