提问人:Steve 提问时间:11/28/2017 更新时间:7/15/2022 访问量:10762
LNK2019:VS 单元测试中未解析的外部符号
LNK2019: unresolved external symbol in VS unit-testing
问:
我收到标题中所述的错误。我确保了以下几点:
- 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>
详细错误:
- LNK2019未解析的外部符号“public: __cdecl LifeLib::StornoTafel::StornoTafel(void)” (??0StornoTafel@LifeLib@@QEAA@XZ) 在函数“public: __cdecl AggSelTester::AggSelTest::AggSelTest(void)“ (??0AggSelTest@AggSelTester@@QEAA@XZ)
- 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)
- 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)
它们为什么会出现?
答:
根据我的经验,有两件事会出现这个错误,要么在你使用的软件中,你没有正确地把它包含在你的项目中,要么出现链接错误,或者你的项目版本和你尝试包含的版本之间可能存在混合。我的意思是,检查它们是否都是 64 岁还是 32 岁。如果它们不相同,则会出现此错误。这些是我知道可能导致这种情况的原因,也可能是其他原因。
评论
我自己找到了答案:我必须包含.cpp文件。
所以修复了错误。但是,我不知道为什么。#include "../LifeLib/StornoTafel.cpp"
我遇到了类似的问题,并通过将输出 .obj 文件添加到测试项目的依赖项中来解决它。
请参阅 MSDN 文档 - https://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef
我知道这已经很晚了,但让我解释一下为什么包含 .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
评论
问题是名称修改,因为您正在从 c++ 调用到 c。 解决方案是使用 Extern C 对 c 代码标头进行包装调用
extern "C" {
#include "../LifeLib/StornoTafel.h"
#include "../LifeLib/testVariables.h"
}
评论