問題描述
我收到此錯誤,但我不知道如何解決.
I get this error, but I don't know how to fix it.
我使用的是 Visual Studio 2013.我將解決方案命名為 MyProjectTest這是我的測試解決方案的結構:
I'm using Visual Studio 2013. I made the solution name MyProjectTest This is the structure of my test solution:
-function.h
#ifndef MY_FUNCTION_H
#define MY_FUNCTION_H
int multiple(int x, int y);
#endif
-function.cpp
#include "function.h"
int multiple(int x, int y){
return x*y;
}
-main.cpp
#include <iostream>
#include <cstdlib>
#include "function.h"
using namespace std;
int main(){
int a, b;
cin >> a >> b;
cout << multiple(a, b) << endl;
system("pause");
return 0;
}
我是初學者;這是一個簡單的程序,它運行沒有錯誤.網上看了看,對單元測試產生了興趣,于是創建了一個測試項目:
I'm a beginner; this is a simple program and it runs without error. I read on the Internet and became interested in the unit test, so I created a test project:
菜單文件 → 新建 → 項目... → 已安裝 → 模板 → Visual C++ → 測試 → 本機單元測試項目 →
Menu File → New → Project... → Installed → Templates → Visual C++ → Test → Native Unit Test Project →
名稱:UnitTest1
解決方案:添加到解決方案
然后位置自動切換到當前打開解決方案的路徑.
Then the location auto-switched to the path of the current open solution.
這是解決方案的文件夾結構:
This is the folder structure of the solution:
我只編輯了文件unittest1.cpp:
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../MyProjectTest/function.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestEqual)
{
Assert::AreEqual(multiple(2, 3), 6);
// TODO: Your test code here
}
};
}
但我明白了:
錯誤 LNK2019:未解析的外部符號.
error LNK2019: unresolved external symbol.
我知道缺少函數 multiple 的實現.我試圖刪除 function.cpp 文件,我用定義替換了聲明,它運行了.但是不建議將聲明和定義寫在同一個文件中.
I know that the implementation of function multiple is missing. I tried to delete the function.cpp file and I replaced the declaration with the definition, and it ran. But writing both declaration and definition in the same file is not recommended.
如何在不這樣做的情況下修復此錯誤?我應該用文件 unittest.cpp 中的 #include "../MyProjectTest/function.cpp"
替換它嗎?
How can I fix this error without doing that? Should I replace it with #include "../MyProjectTest/function.cpp"
in file unittest.cpp?
推薦答案
一種選擇是將 function.cpp
包含在您的 UnitTest1
項目中,但這可能不是最理想的解決方案結構.對您的問題的簡短回答是,在構建 UnitTest1
項目時,編譯器和鏈接器不知道 function.cpp
存在,并且也沒有任何鏈接包含multiple
的定義.解決此問題的一種方法是使用鏈接庫.
One option would be to include function.cpp
in your UnitTest1
project, but that may not be the most ideal solution structure. The short answer to your problem is that when building your UnitTest1
project, the compiler and linker have no idea that function.cpp
exists, and also have nothing to link that contains a definition of multiple
. A way to fix this is making use of linking libraries.
由于您的單元測試在不同的項目中,我假設您的意圖是使該項目成為一個獨立的單元測試程序.如果您正在測試的函數位于另一個項目中,則可以將該項目構建為動態或靜態鏈接的庫.靜態庫在構建時鏈接到其他程序,擴展名為.lib
,動態庫在運行時鏈接,擴展名為.dll
.對于我的回答,我更喜歡靜態庫.
Since your unit tests are in a different project, I'm assuming your intention is to make that project a standalone unit-testing program. With the functions you are testing located in another project, it's possible to build that project to either a dynamically or statically linked library. Static libraries are linked to other programs at build time, and have the extension .lib
, and dynamic libraries are linked at runtime, and have the extension .dll
. For my answer I'll prefer static libraries.
您可以通過在項目屬性中更改您的第一個程序來將其轉換為靜態庫.在常規"選項卡下應該有一個選項,將項目設置為生成可執行文件 (.exe
).您可以將其更改為 .lib
..lib
文件將構建到與 .exe
相同的位置.
You can turn your first program into a static library by changing it in the projects properties. There should be an option under the General tab where the project is set to build to an executable (.exe
). You can change this to .lib
. The .lib
file will build to the same place as the .exe
.
在您的 UnitTest1
項目中,您可以轉到其屬性,然后在類別 Additional Library Directories 的 Linker 選項卡下,添加 MyProjectTest
構建的路徑.然后,對于鏈接器 - 輸入選項卡下的附加依賴項,添加靜態庫的名稱,很可能是 MyProjectTest.lib
.
In your UnitTest1
project, you can go to its properties, and under the Linker tab in the category Additional Library Directories, add the path to which MyProjectTest
builds. Then, for Additional Dependencies under the Linker - Input tab, add the name of your static library, most likely MyProjectTest.lib
.
這應該允許您的項目構建.請注意,這樣做后,MyProjectTest
將不會成為獨立的可執行程序,除非您根據需要更改其構建屬性,這會不太理想.
That should allow your project to build. Note that by doing this, MyProjectTest
will not be a standalone executable program unless you change its build properties as needed, which would be less than ideal.
這篇關于如何解決錯誤 LNK2019:未解析的外部符號 - 函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!