Http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs
Posted February 26th, 2009 by earnie
Todo: reformat to new wiki syntax.
!!! [Minimalist GNU for Windows | http://www.mingw.org
]
!! Msvc and mingw DLLs
Assume we have a testdll. H, testdll. C, and testmain. C. In the first
Case, we will compile testdll. C with mingw, and let the msvc-compiled
Testmain call it. You shoshould use
gcc -shared -o testdll.dll testdll.c -Wl,--output-def,testdll.def,--out-implib,libtestdll.a
To produce the DLL and Def files. msvc cannot use the mingw library,
But since you have already the def file you may easily produce one
The Microsoft lib tool:
lib /machine:i386 /def:testdll.def
Once you have testdll. Lib, It is trivial to produce the executable with msvc:
cl testmain.c testdll.lib
Now for mingw programs calling an msvc DLL. We have two methods. One
Way is to specify the Lib files directly on the command line after
Main program. For example, after
cl /LD testdll.c
Use
gcc -o testmain testmain.c testdll.lib
The other way is to produce the. A files for GCC. For _ cdecl
Functions (in most cases), it is simple: you only need to apply
Reimp tool from Anders norlander (since his Web site is no longer
Available, you may choose to download
[This version | http://jrfonseca.dyndns.org/projects/gnu-win32/software/reimp/index.html
]
Enhanced by Jose Fonseca ):
reimp testdll.lib
gcc -o testmain testmain.c -L. -ltestdll
However, for _ stdcall functions, the above method does not work.
For msvc will prefix an underscore to _ stdcall functions while mingw
Will not. the right way is to produce the def file using the pexports
Tool already ded in the mingw-utils package and filter off the first
Underscore by sed:
pexports testdll.dll | sed "s/^_//" > testdll.def
Then, when using dlltool to produce the import library, add '-U' to the command line:
dlltool -U -d testdll.def -l libtestdll.a
And now, you can proceed in the usual way:
gcc -o testmain testmain.c -L. -ltestdll
Hooray, we got it.
This Guide
May also be helpful.
Here
Is an example of creating a. Lib file from a mingw DLL, in order to be able to use it from msvc.