windows - 链接 : . a、.lib 和 .def 文件

标签 windows dll loader

我正在使用 GNU binutils 在 Windows 上从程序集构建一个 dll。

我知道 dll 可以在加载可执行文件时加载,也可以在运行时加载(使用 LoadLibrary api 调用)。

对于加载时加载,我似乎只需要 dll 文件:不需要 .a、.lib 或 .def 文件。我想知道这些文件格式代表什么以及它们的用途是什么。

我所知道的和一些具体问题:

  • .a 是 Unix 上静态库常用的扩展名。 .a 文件由 --out-implib 生成GNU ld 的选项。据说它是一个“导入库”,这很公平。那么问题是“如果我在链接时不需要它,导入库对我有什么好处?”

  • .lib 是 Windows 上用于静态库的扩展名,根据维基百科,它也被用作 Windows 下的“导入库”,所以我强烈怀疑它们只是 binutils 调用的另一个名称。一个文件。真/假?

  • 我能找到的所有页面都指出 .def 文件列出了 dll 的导出符号。这不是有点类似于“导入库”应该做的事情吗?

  • 此外,我阅读了 here使用 .def 文件是在源文件中手动指定导出的替代方法(我这样做了)。但我还记得读过(找不到引用).def 文件提供了一个索引( ordinal )到导出的符号中,允许更快的运行时加载。是这样吗?

最佳答案

Linux 上的静态库具有.a 文件扩展名。 Windows 上的静态库具有 .lib 文件扩展名。 Windows 上的动态库具有 .dll 扩展名;为了链接 DLL,需要一个导入库。导入库是一个静态库。它包含加载 DLL 所需的代码。现在您正在使用 GCC(不是 cl.exe)在 Windows 上进行编译。 GCC 对导入库有另一个文件扩展名约定,它“应该称为 *.dll.a 或 *.a”,如 doc for the --out-implib 中所述。你提到的。

导入库(.lib 与 MSVC 或 .dll.a 与 GCC)静态库:它们包含加载的代码动态链接库。 I had the same question the other day.

DLL 可能具有导出的函数和未导出的函数。导入库必须知道哪些函数被导出,哪些没有。告诉它的方法之一是 DEF 文件。

When building the DLL, the linker uses the .def file to create an export (.exp) file and an import library (.lib) file. The linker then uses the export file to build the DLL file. Executables that implicitly link to the DLL link to the import library when they are built. -- MSDN: Exporting from a DLL Using DEF Files

另见 MSDN: Exporting Functions from a DLL by Ordinal Rather Than by Name , 这应该可以回答您关于按索引或序号导出的最后一个问题。

关于windows - 链接 : . a、.lib 和 .def 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6422478/

相关文章:

java - AsyncTaskLoader 未启动

Windows 服务

dll 和应用程序之间的 C++ extern 使用

javascript - mockjax 加载器 - 停止运行特定功能

c++ - 将静态库与 dll 链接的正确方法

C++ Qt : catching a segfault from dll

android - 为什么我们已经有了/data/dalvik-cache,还要把*.odex文件放在/system/app下?

c++ - 在 Windows API 中检查文件大小的规范方法?

windows - 下拉组合框时的工具提示

c++ - 既然我有 msys2,我可以删除之前在 mingw 上安装的实例吗?