如何将DLL链接到我的项目?错误LNK2019:未解析的外部符号 [英] How do I link a DLL to my project? error LNK2019: unresolved external symbol

查看:406
本文介绍了如何将DLL链接到我的项目?错误LNK2019:未解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  bar.obj :error LNK2019:未解析的外部符号SomeFunction 

这样显然没有找到函数实现。 p>

我需要做些什么来帮助编译器找到DLL中的定义并将它们与.h文件关联?

我看过一些关于 __ declspec(dllexport) __ declspec(dllimport)

解决方案

您应该已经收到了文件。你需要在运行时的DLL,.h文件与导出的函数的声明,你已经有了。还有一个.lib文件,这个DLL的导入库。这是链接器所需要的,所以它知道如何添加函数到程序的导入表。



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

  #includefoo.h
# pragma注释(lib,foo.lib)

这适用于MSVC,从不是。将.lib文件复制到项目目录或指定完整路径。


I have a file foo.h that 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:

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?

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

解决方案

You should have received at least three files 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.

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:

#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.

这篇关于如何将DLL链接到我的项目?错误LNK2019:未解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆