[英]LNK2019: unresolved external symbol in VS unit-testing
我收到標題中所述的錯誤。 我確保了以下幾點:
- 包含目錄、包含庫和附加包含目錄設置正確
- 在屬性中,子系統設置為 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>
詳細錯誤:
- 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(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)
- 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)
它們為什么會出現?
我自己找到了答案:我必須包含 .cpp 文件。
所以#include "../LifeLib/StornoTafel.cpp"
修復了錯誤。 但是,我不知道為什么。
我知道這已經很晚了,但讓我解釋一下為什么包含 .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
我有類似的問題,並通過將輸出 .obj 文件添加到測試項目的依賴項來修復它。
請參閱 MSDN 文檔 - https://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef
根據我的經驗,有兩件事會導致此錯誤出現,要么是在您使用的軟件中,您沒有將其正確包含在您的項目中,因此出現了鏈接錯誤,或者您的項目和版本之間可能存在混合您嘗試包含的那個。 我的意思是,檢查它們是 64 還是 32。如果它們不相同,則會出現此錯誤。 這些是我所知道的可能導致這種情況的原因,也可能是其他原因。
問題是名稱修改,因為您是從 c++ 調用 c。 解決方案是使用 Extern C 包裝對 c 代碼頭的調用
extern "C" {
#include "../LifeLib/StornoTafel.h"
#include "../LifeLib/testVariables.h"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.