如何在 Visual Studio C++ 中使用第三方 DLL 文件?

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

How do I use a third-party DLL file in Visual Studio C++?

c++visual-studiowinapidll

提问by Zombies

I understand that I need to use LoadLibrary(). But what other steps do I need to take in order to use a third-party DLL file?

我知道我需要使用 LoadLibrary()。但是为了使用第三方 DLL 文件,我还需要采取哪些其他步骤?

I simply jumped into C++ and this is the only part that I do not get (as a Java programmer). I am just looking into how I can use a QtLibrary and tesseract-ocr, yet the process makes no sense to me and is so difficult to google.

我只是跳入 C++,这是我唯一没有得到的部分(作为 Java 程序员)。我只是在研究如何使用Qt库和 tesseract-ocr,但这个过程对我来说毫无意义,而且很难用谷歌搜索。

How do I tell the compiler of the functions that I am using? Should there be an include file from the third-party vendor?

我如何告诉编译器我正在使用的函数?是否应该有来自第三方供应商的包含文件?

采纳答案by Mark Ransom

As everyone else says, LoadLibrary is the hard way to do it, and is hardly ever necessary.

正如其他人所说,LoadLibrary 很难做到,而且几乎没有必要。

The DLL should have come with a .lib file for linking, and one or more header files to #include into your sources. The header files will define the classes and function prototypes that you can use from the DLL. You will need this even if you use LoadLibrary.

DLL 应该带有一个用于链接的 .lib 文件,以及一个或多个头文件以 #include 到您的源代码中。头文件将定义您可以从 DLL 中使用的类和函数原型。即使您使用 LoadLibrary,您也将需要它。

To link with the library, you might have to add the .lib file to the project configuration under Linker/Input/Additional Dependencies.

要与库链接,您可能必须将 .lib 文件添加到链接器/输入/附加依赖项下的项目配置中。

回答by rich p

To incorporate third-party DLLs into my VS 2008 C++ project I did the following (you should be able to translate into 2010, 2012 etc.)...

要将第三方 DLL 合并到我的 VS 2008 C++ 项目中,我执行了以下操作(您应该能够转换为 2010、2012 等)...

I put the header files in my solution with my other header files, made changes to my code to call the DLLs' functions (otherwise why would we do all this?). :^) Then I changed the build to link the LIB code into my EXE, to copy the DLLs into place, and to clean them up when I did a 'clean' - I explain these changes below.

我将头文件和其他头文件放在我的解决方案中,对我的代码进行了更改以调用 DLL 的函数(否则我们为什么要这样做?)。:^) 然后我更改了构建以将 LIB 代码链接到我的 EXE 中,将 DLL 复制到位,并在我执行“清理”时清理它们 - 我将在下面解释这些更改。

Suppose you have 2 third-party DLLs, A.DLL and B.DLL, and you have a stub LIB file for each (A.LIB and B.LIB) and header files (A.H and B.H).

假设您有 2 个第三方 DLL,A.DLL 和 B.DLL,并且每个都有一个存根 LIB 文件(A.LIB 和 B.LIB)和头文件(AH 和 BH)。

  • Create a "lib" directory under your solution directory, e.g. using Windows Explorer.
  • Copy your third-party .LIB and .DLL files into this directory
  • 在您的解决方案目录下创建一个“lib”目录,例如使用 Windows 资源管理器。
  • 将第三方 .LIB 和 .DLL 文件复制到此目录中

(You'll have to make the next set of changes once for each source build target that you use (Debug, Release).)

(您必须为您使用的每个源构建目标(调试、发布)进行下一组更改。)

  1. Make your EXE dependent on the LIB files

    • Go to Configuration Properties -> Linker -> Input -> Additional Dependencies, and list your .LIB files there one at a time, separated by spaces: A.LIB B.LIB
    • Go to Configuration Properties -> General -> Additional Library Directories, and add your "lib" directory to any you have there already. Entries are separated by semicolons. For example, if you already had $(SolutionDir)fodderthere, you change it to $(SolutionDir)fodder;$(SolutionDir)libto add "lib".
  2. Force the DLLs to get copied to the output directory

    • Go to Configuration Properties -> Build Events -> Post-Build Event
    • Put the following in for Command Line (for the switch meanings, see "XCOPY /?" in a DOS window):

    XCOPY "$(SolutionDir)"\lib\*.DLL "$(TargetDir)" /D /K /Y

    • You can put something like this for Description:

    Copy DLLs to Target Directory

    • Excluded From Build should be No. Click OK.
  3. Tell VS to clean up the DLLs when it cleans up an output folder:

    • Go to Configuration Properties -> General -> Extensions to Delete on Clean, and click on "..."; add *.dllto the end of the list and click OK.
  1. 使您的 EXE 依赖于 LIB 文件

    • 转到配置属性 -> 链接器 -> 输入 -> 附加依赖项,并一次列出一个 .LIB 文件,以空格分隔: A.LIB B.LIB
    • 转到配置属性 -> 常规 -> 附加库目录,并将您的“lib”目录添加到您已有的任何目录中。条目由分号分隔。例如,如果您已经在$(SolutionDir)fodder那里,则将其更改$(SolutionDir)fodder;$(SolutionDir)lib为添加“lib”。
  2. 强制将 DLL 复制到输出目录

    • 转到配置属性 -> 构建事件 -> 构建后事件
    • 将以下内容放入命令行(有关开关含义,请参阅 DOS 窗口中的“XCOPY /?”):

    XCOPY "$(SolutionDir)"\lib\*.DLL "$(TargetDir)" /D /K /Y

    • 您可以将这样的内容作为说明:

    Copy DLLs to Target Directory

    • 从构建中排除应该是No. 单击OK
  3. 告诉 VS 在清理输出文件夹时清理 DLL:

    • 转到配置属性 -> 常规 -> 在清理时删除的扩展,然后单击“...”;添加*.dll到列表末尾并单击OK

回答by Laserallan

These are two ways of using a DLL file in Windows:

这是在 Windows 中使用 DLL 文件的两种方法:

  1. There is a stub library (.lib) with associated header files. When you link your executable with the lib-file it will automatically load the DLL file when starting the program.

  2. Loading the DLL manually. This is typically what you want to do if you are developing a plugin system where there are many DLL files implementing a common interface. Check out the documentation for LoadLibraryand GetProcAddressfor more information on this.

  1. 有一个带有相关头文件的存根库 (.lib)。当您将可执行文件与 lib 文件链接时,它会在启动程序时自动加载 DLL 文件。

  2. 手动加载 DLL。如果您正在开发一个插件系统,其中有许多 DLL 文件实现了一个公共接口,那么这通常是您想要做的。查看LoadLibraryGetProcAddress的文档以获取更多信息。

For Qt I would suspect there are headers and a static library available that you can include and link in your project.

对于 Qt,我怀疑有可用的头文件和静态库,您可以在项目中包含和链接它们。

回答by drby

In order to use Qt with dynamic linking you have to specify the libfiles (usually qtmaind.lib, QtCored4.liband QtGuid4.libfor the "Debug" configration) in
Properties ? Linker ? Input ? Additional Dependencies.

为了使用Qt动态链接时,你必须指定lib文件(通常qtmaind.libQtCored4.libQtGuid4.lib在“调试” CONFIGRATION)
Properties ? Linker ? Input ? Additional Dependencies

You also have to specify the path where the libs are, namely in
Properties ? Linker ? General ? Additional Library Directories.

您还必须指定库所在的路径,即在
Properties ? Linker ? General ? Additional Library Directories.

And you need to make the corresponding .dlls are accessible at runtime, by either storing them in the same folder as your .exeor in a folder that is on your path.

并且您需要使相应的.dlls 在运行时可访问,方法是将它们存储在与您.exe的文件夹相同的文件夹中或您的路径上的文件夹中。

回答by Stu Mackellar

You only need to use LoadLibrary if you want to late bind and only resolve the imported functions at runtime. The easiest way to use a third party dll is to link against a .lib.

如果您想延迟绑定并且只在运行时解析导入的函数,您只需要使用 LoadLibrary。使用第三方 dll 的最简单方法是链接到 .lib。



In reply to your edit:

回复您的编辑:

Yes, the third party API should consist of a dll and/or a lib that contain the implementation and header files that declares the required types. You need to know the type definitions whichever method you use - for LoadLibrary you'll need to define function pointers, so you could just as easily write your own header file instead. Basically, you only need to use LoadLibrary if you want late binding. One valid reason for this would be if you aren't sure if the dll will be available on the target PC.

是的,第三方 API 应该包含一个 dll 和/或一个库,其中包含声明所需类型的实现和头文件。无论使用哪种方法,您都需要知道类型定义 - 对于 LoadLibrary,您需要定义函数指针,因此您可以轻松地编写自己的头文件。基本上,如果您想要后期绑定,您只需要使用 LoadLibrary。一个有效的原因是如果您不确定目标 PC 上是否可以使用 dll。

回答by ChrisW

I'f you're suppsed to be able to use it, then 3rd-party library should have a *.lib file as well as a *.dll file. You simply need to add the *.lib to the list of input file in your project's 'Linker' options.

如果您可以使用它,那么第 3 方库应该有一个 *.lib 文件和一个 *.dll 文件。您只需将 *.lib 添加到项目“链接器”选项中的输入文件列表中。

This *.lib file isn't necessarily a 'static' library (which contains code): instead a *.lib can be just a file that links your executable to the DLL.

这个 *.lib 文件不一定是“静态”库(其中包含代码):相反,*.lib 可以只是将可执行文件链接到 DLL 的文件。