gpt4 book ai didi

c++ - 为什么Qt Creator不用.lib和LoadLibrary()也能正确链接dll?

转载 作者:行者123 更新时间:2023-11-30 04:47:43 25 4
gpt4 key购买 nike

通常当我们链接到一个dll时,我们必须同时使用.lib,这样链接器才能知道它是动态链接。然而,在 Qt Creator 中,我们可以在没有它的情况下做到这一点。

我是按照youtube上的说明操作的,简单的过程如下:

  1. 准备一个TestMessagePack.dll,由GCC编译;
  2. 将dll复制到debug文件夹,exe所在位置。
  3. 添加下面的代码,其中也指示包含文件所在的位置。
INCLUDEPATH += "C:\Users\***\Desktop\Brand New\CMakeStuff\include\qmsgpack"

LIBS += "C:\Users\***\Desktop\Brand New\CMakeStuff\bin\TestMessagePack.dll"
  1. 构建并运行。

如果没有第 2 步,链接器将失败。很明显,Qt Creator 可以链接到没有 .lib 的 dll。但是链接器怎么知道没有 .lib 文件呢?

我要知道里面的原理

最佳答案

您正在使用各种 MinGW 之一?? GCC 的 Windows 端口。在 Windows 上,与 Microsoft 链接器不同,GCC 工具链的链接器 (ld) 支持 DLL 的直接链接这需要代理 .lib 导入库。 Documentation :

direct linking to a dll

The cygwin/mingw ports of ld support the direct linking, including data symbols, to a dll without the usage of any import libraries.

...

MinGW: Specify the libraries for the linker to use

MinGW supports libraries named according to the ".lib" and ".dll" conventions, in addition to the normal "lib.a" convention common on *nix systems.

...

这种行为是由 MingGW 给出的??您正在使用的链接器。这意味着动态库与 ld 的链接可以在 Windows 上以与在类 unix 操作系统上相同的方式完成,其中不存在导入库。 Qt 不相关。

关于c++ - 为什么Qt Creator不用.lib和LoadLibrary()也能正确链接dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56123457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com