Msvc and mingw DLLs

Source: Internet
Author: User

Http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs

 

Posted February 26th, 2009 by earnie
  • DLL
  • FAQ
  • Msvc

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.