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

LNK2019: unresolved external symbol in VS unit-testing

提问人:Steve 提问时间:11/28/2017 更新时间:7/15/2022 访问量:10762

问:

我收到标题中所述的错误。我确保了以下几点:
- Include 目录、include 库和其他 include 目录设置正确
- 在属性中,Subsystem 设置为 CONSOLE

对我的代码的注释: LifeLib 是一个包含我想测试一些方法的类的项目。这些类在命名空间 LifeLib 中定义。其中之一是 StornoTafel。testVariables 未在任何命名空间中定义。
我收到 3 次链接错误,用于 StornoTafel 中的 2 个构造函数和 1 个方法(在代码中注明)。

//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(类 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) 函数中引用 “公共:无效__cdecl AggSelTester::AggSelTest::init(void)” (?init@AggSelTest@AggSelTester@@QEAAXXZ)

它们为什么会出现?

C++ Visual-Studio 单元测试 链接器错误 LNK2019

评论


答:

1赞 José Matos 11/28/2017 #1

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

评论

0赞 Steve 11/28/2017
一切都以 64 位运行,所以不应该是这样。正如我所说,我确信我输入了正确的包含路径等。
16赞 Steve 11/28/2017 #2

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

4赞 Eric Lin 6/22/2018 #3

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

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

11赞 Liam 9/21/2018 #4

我知道这已经很晚了,但让我解释一下为什么包含 .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赞 Vegeta 4/4/2021
这不可能是一个实际的解决方案......这需要包含大量 cpp 文件
0赞 user1633272 8/14/2021
这并不能解释为什么这种情况只发生在 x64 配置中。
1赞 Chaviv Perlman 7/15/2022 #5

问题是名称修改,因为您正在从 c++ 调用到 c。 解决方案是使用 Extern C 对 c 代码标头进行包装调用

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