Linking : .a, .lib and .def files

46,496

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.

Share:
46,496
Norswap
Author by

Norswap

Updated on May 26, 2020

Comments

  • Norswap
    Norswap about 4 years

    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 ?

  • Norswap
    Norswap about 13 years
    Ok, thanks :) The only question that remains is that "in order to link against a DLL, an import library is required" is not true for me : I can link using only the dll. Is that maybe because I specify which symbol are exported inside the dll (using "-export:function" in assembler which seems to be equivalent to "__declspec( dllexport ) function" in C) ? To me it would seem a replacement of the .def file, not the import library.
  • Lumi
    Lumi about 13 years
    I have no idea what can be done in assembler nor what exactly you're doing. In a C program, you can also call LoadLibrary() and then GetProcAddress() to load the DLL at runtime. So I guess it's also possible in assembler.
  • Norswap
    Norswap about 13 years
    Even without using those calls it isn't necessary. You can check a simple example here : norswap.droppages.com/asm/win_bundling/dll
  • Lumi
    Lumi about 13 years
    Nice sample! +1 Found the answer to your question. "The cygwin/mingw ports of ld support the direct linking, including data symbols, to a dll without the usage of any import libraries. ... Linking directly to a dll uses no extra command-line switches other than -L' and -l', because ld already searches for a number of names to match each library. ... Given the improvements in speed and memory usage, one might justifiably wonder why import libraries are used at all. There are three reasons ..." Supported since at least 2002!
  • Norswap
    Norswap over 10 years
    Updated link for my last comment: github.com/norswap/asm-exemples/tree/master/dll
  • CYB3R
    CYB3R over 6 years
    @Norswap The link seems dead
  • truthadjustr
    truthadjustr almost 6 years
    what's the use of an 'import library' when you can load a DLL via LoadLibrary() and then retrieve the methods via GetProcAddress() ? Please explain why do I need an import library???
  • wingerse
    wingerse almost 6 years
    @ifelsemonkey As mentioned in the comments, mingw port of ld does not require an import library to link to .dlls. In case you are asking about load vs runtime linking, load time linking is done by the OS linker. At runtime, ur on ur own.