C++ 错误LNK2019:未解析的外部符号-函数如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19886397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:13:45  来源:igfitidea点击:

How to solve the error LNK2019: unresolved external symbol - function?

c++testingerror-handlinglnk2019

提问by phibao37

I get this error, but I don't know how to fix it.

我收到此错误,但我不知道如何解决。

I'm using Visual Studio 2013. I made the solution name MyProjectTestThis is the structure of my test solution:

我使用的是 Visual Studio 2013。我将解决方案命名为MyProjectTest这是我的测试解决方案的结构:

The structure

结构

-function.h

-函数.h

#ifndef MY_FUNCTION_H
#define MY_FUNCTION_H

int multiple(int x, int y);
#endif

-function.cpp

-function.cpp

#include "function.h"

int multiple(int x, int y){
    return x*y;
}

-main.cpp

- 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 in the internet and became interested in the unit test, so I created a test project:

我是初学者;这是一个简单的程序,它运行没有错误。在网上看了看,对单元测试产生了兴趣,于是创建了一个测试项目:

File > New > Project... > Installed > Templates > Visual C++ > Test > Native Unit Test Project >

文件 > 新建 > 项目... > 已安装 > 模板 > Visual C++ > 测试 > 本机单元测试项目 >

Name: UnitTest1Solution: Add to solutionThen the location auto-switched to the path of the current open solution This is the folder structure of the solution:

名称:UnitTest1解决方案:添加到解决方案然后位置自动切换到当前打开的解决方案的路径 这是解决方案的文件夹结构:

Folder structure

文件夹结构

I only edited file unittest1.cpp:

我只编辑了文件 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
        }

    };
}

But I get error LNK2019: unresolved external symbol. I know that the implementation of function multipleis missing. I tried to delete the function.cpp file and I replaced the declaration with the definition, and it run. But writing both declaration and definition in the same file is not recommended. How can I fix this error without doing that? Should I replace with #include "../MyProjectTest/function.cpp"in file unittest.cpp? (I'm not good at English very much. Thanks)

但我收到错误 LNK2019:未解析的外部符号。我知道缺少函数multiple的实现。我试图删除 function.cpp 文件,并用定义替换了声明,然后它运行了。但是不建议在同一个文件中同时编写声明和定义。如何在不这样做的情况下修复此错误?我应该用#include "../MyProjectTest/function.cpp"文件 unittest.cpp替换吗? (我不太擅长英语。谢谢)

采纳答案by kevintodisco

One option would be to include function.cppin your UnitTest1project, but that may not be the most ideal solution structure. The short answer to your problem is that when building your UnitTest1project, the compiler and linker have no idea that function.cppexists, and also have nothing to link that contains a definition of multiple. A way to fix this is making use of linking libraries.

一种选择是包含function.cpp在您的UnitTest1项目中,但这可能不是最理想的解决方案结构。对您的问题的简短回答是,在构建UnitTest1项目时,编译器和链接器不知道function.cpp存在,也没有任何包含multiple. 解决此问题的一种方法是使用链接库。

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.

由于您的单元测试在不同的项目中,我假设您的意图是使该项目成为一个独立的单元测试程序。如果您正在测试的函数位于另一个项目中,则可以将该项目构建为动态或静态链接的库。静态库在构建时链接到其他程序,并具有扩展名.lib,而动态库在运行时链接,并具有扩展名.dll. 对于我的回答,我更喜欢静态库。

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 .libfile will build to the same place as the .exe.

您可以通过在项目属性中更改您的第一个程序将其转换为静态库。在“常规”选项卡下应该有一个选项,将项目设置为生成可执行文件 ( .exe)。您可以将其更改为.lib. 该.lib文件将构建到与.exe.

In your UnitTest1project, you can go to its properties, and under the Linker tab in the category Additional Library Directories, add the path to which MyProjectTestbuilds. Then, for Additional Dependencies under the Linker - Input tab, add the name of your static library, most likely MyProjectTest.lib.

在您的UnitTest1项目中,您可以转到其属性,并在“附加库目录”类别的“链接器”选项卡下,添加MyProjectTest构建路径。然后,对于链接器 - 输入选项卡下的其他依赖项,添加静态库的名称,最有可能是MyProjectTest.lib.

That should allow your project to build. Note that by doing this, MyProjectTestwill not be a standalone executable program unless you change its build properties as needed, which would be less than ideal.

这应该允许您的项目构建。请注意,通过这样做,MyProjectTest除非您根据需要更改其构建属性,否则将不会成为独立的可执行程序,这将不太理想。

回答by Niyaz Ivanov

In Visual Studio solution tree right click on the project 'UnitTest1' then Add -> Existing item -> choose the file ../MyProjectTest/function.cpp

在 Visual Studio 解决方案树中右键单击项目“UnitTest1”,然后添加 -> 现有项目 -> 选择文件 ../MyProjectTest/function.cpp

回答by harrygg

Since I want my project to compile to a standalone EXE, I linked the UnitTest project to the function.obj file generated from the function.cpp and it works. Right click on the 'UnitTest1' project > Configuration Properties > Linker > Input > Additional Dependencies > add "..\MyProjectTest\Debug\function.obj"

由于我希望我的项目编译为独立的 EXE,我将 UnitTest 项目链接到从 function.cpp 生成的 function.obj 文件并且它可以工作。右键单击“UnitTest1”项目>配置属性>链接器>输入>附加依赖项>添加“..\MyProjectTest\Debug\function.obj”

回答by Kevin Dill

I just ran into this problem in Visual Studio 2013. Apparently now, having two projects in the same solution and setting the the dependencies is not enough. You need to add a project reference between them. To do that:

我刚刚在 Visual Studio 2013 中遇到了这个问题。显然现在,在同一个解决方案中有两个项目并设置依赖项是不够的。您需要在它们之间添加一个项目引用。要做到这一点:

  1. Right-click on the project in the solution explore
  2. Click Add => References...
  3. Click the Add New Reference button
  4. Check the boxes for the projects that this project relies on
  5. Click OK
  1. 右键单击解决方案探索中的项目
  2. 单击添加 => 引用...
  3. 单击添加新引用按钮
  4. 选中此项目依赖的项目的框
  5. 单击确定

回答by cahit beyaz

it turned out i was using .c files with .cpp files. renaming .c to .cpp solved my problem.

原来我在使用 .c 文件和 .cpp 文件。将 .c 重命名为 .cpp 解决了我的问题。

回答by zar

Another way you can get this linker error (as I was) is if you are exporting an instanceof a class from a dll but have not declared that class itself as import/export.

另一种获得此链接器错误的方法(就像我一样)是,如果您从 dll导出类的实例,但尚未将该类本身声明为导入/导出。

 #ifdef  MYDLL_EXPORTS 
    #define DLLEXPORT __declspec(dllexport)  
 #else
    #define DLLEXPORT __declspec(dllimport)  
 #endif

class DLLEXPORT Book // <--- this class must also be declared as export/import
{
public: 
    Book();
    ~Book();
    int WordCount();
};

DLLEXPORT extern Book book; // <-- This is what I really wanted, to export book object

So even though primarily I was exporting just an instanceof the Book class called bookabove, I had to declare the Bookclass as export/import class as well otherwise calling book.WordCount()in the other dll was causing a link error.

因此,即使主要是导出上面调用的 Book 类的一个实例book,我也必须将该Book类声明为导出/导入类,否则调用book.WordCount()另一个 dll 会导致链接错误。

回答by Mushy

I just discovered that LNK2019occurs during compilation in Visual Studio 2015 if forgetting to provide a definition for a declared function inside a class.

我刚刚发现LNK2019在 Visual Studio 2015 编译期间如果忘记为类中声明的函数提供定义,就会发生这种情况。

The linker error was highly cryptic but I narrowed down what was missing by reading through the error and provided the definition outside the class to clear this up.

链接器错误非常神秘,但我通过阅读错误缩小了缺失的范围,并在类外提供了定义来清除它。

回答by Enyx

This happened to me so I thought I might share my solution, as simple as it was:

这发生在我身上,所以我想我可以分享我的解决方案,就像它一样简单:

Check the Character Set of both projects in Configuration Properties-> General-> Character Set

Configuration Properties-> General-> Character Set 中检查两个项目的Character Set

My UnitTest project was using the default Character Set Multi-Bytewhile my libs where in Unicode.
My function was using a TCHARas parameter. As a result in my lib my TCHARwas transformedinto a WCHARbut it was a char*on my UnitTest: the symbol were different because the parameters were really not the same in the end.

我的 UnitTest 项目使用默认的字符集多字节,而我的库使用Unicode
我的函数使用TCHAR作为参数。结果在我的库中,我的TCHAR转换WCHAR,但在我的 UnitTest 中它是一个char*:符号不同,因为最终参数真的不一样。

回答by user276648

In Visual Studio 2017 if you want to test public members, simply put your real project and test project in the same solution, and add a reference to your real project in the test project.

在 Visual Studio 2017 中,如果要测试公共成员,只需将您的真实项目和测试项目放在同一个解决方案中,并在测试项目中添加对您的真实项目的引用。

See C++ Unit Testing in Visual Studiofrom the MSDN blog for more details. You can also check Write unit tests for C/C++ in Visual Studioas well as Use the Microsoft Unit Testing Framework for C++ in Visual Studio, the latter being if you need to test non public members and need to put the tests in the same project as your real code.

有关更多详细信息,请参阅MSDN 博客中 Visual Studio 中的 C++ 单元测试。您还可以检查在 Visual Studio 中为 C/C++ 编写单元测试以及在 Visual Studio 中C++使用 Microsoft 单元测试框架,后者是如果您需要测试非公共成员并且需要将测试放在同一个项目中作为您的真实代码。

Note that things you want to test will need to be exported using __declspec(dllexport). See Exporting from a DLL Using __declspec(dllexport)for more details.

请注意,您要测试的内容需要使用__declspec(dllexport). 有关更多详细信息,请参阅使用 __declspec(dllexport) 从 DLL 导出

回答by pivnothebearrrrrrrrr

in my case ,set the cpp file to "C/C++ compiler" in property -> general resolve the LNK2019 error.

就我而言,在属性中将 cpp 文件设置为“C/C++ 编译器”-> 一般解决 LNK2019 错误。