C++ 如何将 DLL 链接到我的项目?错误 LNK2019:未解析的外部符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15090418/
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 19:01:06  来源:igfitidea点击:

How do I link a DLL to my project? error LNK2019: unresolved external symbol

c++windowsfunctiondlllinker

提问by xcdemon05

I have a file foo.hthat has various declarations for functions. All of these functions are implemented in a file foo.dll. However, when I include the .h file and try to use any of the functions, I get the error:

我有一个foo.h包含各种函数声明的文件。所有这些功能都在一个文件中实现foo.dll。但是,当我包含 .h 文件并尝试使用任何函数时,出现错误:

bar.obj : error LNK2019: unresolved external symbol SomeFunction

so obviously the function implementations aren't being found.

所以显然没有找到函数实现。

What do I have to do to help the compiler find the definitions in the DLL and associate them with the .h file?

我该怎么做才能帮助编译器找到 DLL 中的定义并将它们与 .h 文件相关联?

I've seen some stuff about __declspec(dllexport)and __declspec(dllimport)but I still can't figure out how to use them.

我见过的一些东西,__declspec(dllexport)__declspec(dllimport),但我仍然无法弄清楚如何使用它们。

回答by Hans Passant

You should have received at least threefiles from the DLL owner. The DLL which you'll need at runtime, the .h file with the declarations of the exported functions, you already have that. And a .lib file, the import library for the DLL. Which the linker requires so it knows how to add the functions to the program's import table.

您应该至少收到了来自 DLL 所有者的三个文件。您在运行时需要的 DLL,带有导出函数声明的 .h 文件,您已经拥有了。还有一个 .lib 文件,即 DLL 的导入库。链接器需要什么才能知道如何将函数添加到程序的导入表中。

You are missing the step where you told the linker that it needs to link the .lib file. It needs to be added to the linker's Input + Additional Dependencies setting of your project. Or most easily done by writing the linker instruction in your source code:

您错过了告诉链接器它需要链接 .lib 文件的步骤。它需要添加到项目的链接器的 Input + Additional Dependencies 设置中。或者最容易通过在源代码中编写链接器指令来完成:

#include "foo.h"
#pragma comment(lib, "foo.lib")

Which works for MSVC, not otherwise portable but linking never is. Copy the .lib file to your project directory or specify the full path.

这适用于 MSVC,否则不可移植,但链接永远不是。将 .lib 文件复制到您的项目目录或指定完整路径。

回答by DarenW

I just had a similar problem. The solution turned out to be that the DLL was 64 bit, and the simple app using it was 32. I had forgotten to change it to x64 in the Configuration Manager.

我刚刚遇到了类似的问题。解决方案原来是 DLL 是 64 位的,而使用它的简单应用程序是 32 位。我忘记在配置管理器中将其更改为 x64。

回答by Abhineet

  1. You need to specify in front of function definitions __declspec(dllexport) keyword at the time of building the dll
  2. You need to import or load the .dll file into process memory.
  3. You need to acquire the address of function you want to use from that dll.
  1. 构建dll时需要在函数定义前指定__declspec(dllexport)关键字
  2. 您需要将 .dll 文件导入或加载到进程内存中。
  3. 您需要从该 dll 中获取要使用的函数的地址。

Some useful links to get started:: MSDN Documentation, SO, Random

一些有用的入门链接:MSDN 文档SO随机