This page is about how to "call into" an existing DLL, using your mingw program. for information on creating your own DLL library, see DLL.
Usually (read: for all DLLs created with MinGW and also a few others) MinGW links fine against a DLL. No special 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 in linux cross compile). 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 custom def-File. There are several ways to create such a file. In the following some of these will be described:
run '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 starting point 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.
def-file for an external lib created with Delphi
LIBRARY Diverg01.dll EXPORTS Divergences LTDivergences STDivergences
Excerpt from a def-file for IBM's db2cli.dll
LIBRARY DB2CLI.dll EXPORTS SQLAllocConnect@8 SQLAllocEnv@4 SQLAllocHandle@12 SQLAllocStmt@8 SQLBindCol@24 SQLBindFileToCol@32 SQLBindFileToParam@32 SQLBindParam@32 SQLBindParameter@40 SQLBrowseConnect@24 SQLBrowseConnectW@24 SQLBuildDataLink@40 SQLBulkOperations@8 ...
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).