Revision of HOWTO Create an Import Library for a DLL using MinGW from 2010, January 7 - 14:21

Printer-friendly version

Usually (read: for all DLLs created with MinGW and also a few others) MinGW links fine against a DLL. No import library is necessary (see sampleDLL).

However there are situations when it won't. Then one needs a so-called import library to help the linker. The rest of this page gives hints as to how to create such import libraries when there is only the DLL available.

MinGW comes with a handy tool which does most of the work for you:

dlltool.exe (or i586-mingw32-dlltool or something similar). Issue 'dlltool --help' to get a brief description of the available options.

Sometimes dlltool can create an import library from the DLL. But then you wouldn't need an import library since the linker wouldn't need one. So you probably are reading this because you've got a DLL against which MinGW can't link without further help. You will provide this help by creating a def-File. There are several ways to create such a file. In the following some of these will be described:

issue 'dlltool -z somedll.def --export-all-symbol somedll.dll' (without 's)

chances are high that you'll get something like 'dlltool: somedll.dll: no symbols'. However, the resulting def-File is a good startingpoint and probably will need only little additional tweaking.

use your favorite texteditor and create a file somedll.def with the following lines:

<list of external names, one per line>

Here <list of external names> is at least the list of unsatisfied external references the linker did complain about and that are supposed to be in somedll.dll. The list is given one name per line.
Once you've created the def-File (see above) you'll issue

    dlltool -d somedll.def -l libsomedll.a

Depending on the DLL you may want to try one or more of the following:

add option -U

This will prefix all symbols in the import library with an underscore '_'

add option -k

This removes @<n> from the exported names but their calling convention remains stdcall.

add option -A

This adds an alias for all stdcall entries but without the @<n>, i.e. you get both the undecorated names (as with -k) and the decorated names.

For a detailed description of these options and dlltool take a look at the corresponding documentation.

That's it. The resulting file libsomedll.a should satisfy the linker.

Some Examples

def-file for an external lib created with Delphi

LIBRARY Diverg01.dll

Excerpt from a def-file for IBM's db2cli.dll


Note that you can also use the pexports command to help (see the Python example in the FAQ).

Note also that some dll's may just *not be compatible* -- for example if size differences occur between long long in the different compilers, and you attempt to pass out structs that have long longs. MSVC is known to have different lengths than gcc for long double, some of its versions of it have a difference for size_t, as well (other discrepancies may be lurking, as well).

This wiki is not a forum for discussion of usage issues. Please use the list instead. We do not allow creation of comments by anonymous or untrusted users, on any page.
Site Status

Site maintenance performed successfully, site online Dec 13th 12:00 AM Eastern.