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

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:

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:

LIBRARY SOMEDLL.DLL
EXPORT
<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.

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).