windows 链接:.a、.lib 和 .def 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6422478/
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
Linking : .a, .lib and .def files
提问by Norswap
I am building a dll from assembly on Windows using the GNU binutils.
我正在使用 GNU binutils 从 Windows 上的程序集构建一个 dll。
I know that the dll can be either loaded when the executable is loaded or at run-time (using the LoadLibrary api call).
我知道可以在加载可执行文件时或在运行时(使用 LoadLibrary api 调用)加载 dll。
For load-time loading, I seem to be only needing the dll file : no .a, .lib or .def file is needed. I wondered what these file format represent and what purpose do they serve.
对于加载时加载,我似乎只需要 dll 文件:不需要 .a、.lib 或 .def 文件。我想知道这些文件格式代表什么以及它们的用途是什么。
What I know and some specific questions :
我所知道的和一些具体问题:
.a is the extension generally used for static library on Unix. .a files are generated with the --out-impliboption of GNU ld. It is said to be an "import library", fair enough. The question is then "What good is an import library to me if I don't need it when linking ?"
.lib is the extension used for static library on Windows, and according to wikipedia, is also used as "import library" under windows, so I strongly suspect they're just another name for what the binutils call .a files. True/false ?
All pages I can find points that .def files list the exported symbol of the dll. Isn't that somewhat similar to what an "import library" is supposed to do ?
Also, I read herethat using .def files is an alternative to manually specifying exports in the source file (which I did). But I also remember reading (cannot find reference back) .def file supply an index (ordinal) into the exported symbols, allowing for faster run-time loading. Is that so ?
.a 是 Unix 上通常用于静态库的扩展名。.a 文件是使用GNU ld的--out-implib选项生成的。据说它是一个“进口图书馆”,够公平的。那么问题是“如果我在链接时不需要它,那么导入库对我有什么用?”
.lib 是 Windows 上用于静态库的扩展名,根据维基百科,它也用作 Windows 下的“导入库”,所以我强烈怀疑它们只是 binutils 称为 .a 文件的另一个名称。真假 ?
我能找到的所有页面都指出 .def 文件列出了 dll 的导出符号。这不是有点类似于“导入库”应该做的事情吗?
另外,我在这里读到使用 .def 文件是在源文件中手动指定导出的替代方法(我这样做了)。但我也记得阅读(找不到引用) .def 文件在导出的符号中提供索引(序数),允许更快的运行时加载。是这样吗 ?
回答by Lumi
Static libraries on Linux have the .a
file extension. Static libraries on Windows have the .lib
file extension. Dynamic libraries on Windows have the .dll
extension; in order to link against a DLL, an import library is required. The import library is a static library. It contains the code required to load the DLL. Now you're using GCC (not cl.exe
) to compile on Windows. GCC has another file extension convention for import libraries, it "should be called *.dll.a or *.a", as explained in the doc for the --out-implib
you referred to.
Linux 上的静态库具有.a
文件扩展名。Windows 上的静态库具有.lib
文件扩展名。Windows 上的动态库具有.dll
扩展名;为了链接到 DLL,需要一个导入库。导入库是一个静态库。它包含加载 DLL 所需的代码。现在您正在使用 GCC(而不是cl.exe
)在 Windows 上进行编译。GCC 有另一个用于导入库的文件扩展名约定,它“应该被称为 *.dll.a 或 *.a”,如您所指的文档中所述--out-implib
。
Import libraries (.lib
with MSVC or .dll.a
with GCC) arestatic libraries: they contain the code to load the DLL. I had the same question the other day.
导入库(.lib
使用 MSVC 或.dll.a
GCC)是静态库:它们包含加载 DLL 的代码。前几天我也有同样的问题。
A DLL may have functions that are exported and functions that are not exported. An import library has to know which functions are exported and which aren't. One of the means of telling it is a DEF file.
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
构建 DLL 时,链接器使用 .def 文件创建导出 (.exp) 文件和导入库 (.lib) 文件。然后链接器使用导出文件来构建 DLL 文件。在构建时隐式链接到导入库的 DLL 链接的可执行文件。-- MSDN:使用 DEF 文件从 DLL 导出
Also see MSDN: Exporting Functions from a DLL by Ordinal Rather Than by Name, together that should answer your last question on export by index, or ordinal number.
另请参阅MSDN: Exporting Functions from a DLL by Ordinal 而不是按名称,它们一起应该回答您关于按索引或序数导出的最后一个问题。