Linking : .a, .lib and .def files

非 Y 不嫁゛ 提交于 2019-12-18 10:13:15

问题


I am building a dll from assembly on Windows using the GNU binutils.

I know that the dll can be either loaded when the executable is loaded or at run-time (using the LoadLibrary api call).

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.

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-implib option 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 here that 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 ?


回答1:


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.

Import libraries (.lib with MSVC or .dll.a with GCC) are static libraries: they contain the code to load the DLL. I had the same question the other day.

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.

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

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.



来源:https://stackoverflow.com/questions/6422478/linking-a-lib-and-def-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!