How can an MSVC program call an MinGW DLL?

 

MSVC and MinGW DLL Interlinking FAQ <script type="text/javascript"> </script> <script type="text/javascript"> </script>

Q. How can an MSVC program call an MinGW DLL, and vice versa?

A. 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 should 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 by 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 the main program (in newer MinGW versions; MinGW GCC 2.95.2 is reported not to work). 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 the reimp tool (the original site is unavailable now, but you may download here a version enhanced by José Fonseca):

reimp testdll.lib
gcc -o testmain testmain.c -L. -ltestdll

However, the above method does not work with __stdcall functions. 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 (downloadable here if not found elsewhere) 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. And that is the end of this FAQ.

### 回答1: Mingw是一个在Windows系统上运行的开源C/C++编译器集合,而MSVC是微软的Visual C++编译器。要使用Mingw调用MSVC编译出的DLL,需要注意以下几点: 1. 确保DLL的函数接口使用了C或者标准C++风格的导出方式,避免使用MSVC特有的扩展。 2. 确保Mingw的头文件和库文件与MSVC兼容,这样在编译时才能正确地解析和链接函数。 3. 在Mingw的编译选项中,添加MSVC编译生成的DLL的导入库文件。可以使用"-l"选项指定导入库的路径和名称。比如,如果DLL的导入库文件名为"example.lib",则编译命令可以添加"-lexample"选项。 4. 在运行时,确保DLL和应用程序的运行环境相匹配,包括操作系统版本、依赖的运行库版本等。特别是,如果MSVC编译时使用了动态运行时库(如MSVCRT.dll),则需要确保系统上有正确的运行时库版本。 总的来说,Mingw可以通过正确的配置和编译选项来调用MSVC编译出的DLL,但是要注意确保导出接口的兼容性和运行时环境的一致性,以避免编译、链接和运行时的问题。 ### 回答2: Mingw是一种基于GNU工具链的开发环境,而MSVC是微软的编译器。在使用Mingw调用MSVC编译出的DLL时,需要注意一些细节。 首先,要确保DLL的导出函数采用了标准的C调用约定,即使用`extern "C"`进行声明。这是因为Mingw默认使用C调用约定,而MSVC默认使用C++调用约定。 其次,要确保使用Mingw的编译器时,指定了正确的头文件路径和库文件路径。通常情况下,MSVC编译器生成的库文件的扩展名为`.lib`,而Mingw使用的是`.a`。因此在编译时要使用正确的库文件。 另外,还需要注意编译时使用相同的编译选项和标志。如果MSVC编译器使用了特定的编译选项或标志,那么在使用Mingw编译时也要保持一致,以免出现不兼容或不可预料的错误。 最后,在链接时需要确保引入了正确的库文件。MingwMSVC在库文件的命名规则上可能有一些差异,要确保使用了正确的库文件。 综上所述,要使用Mingw调用MSVC编译出的DLL,需要注意C调用约定、头文件路径、库文件路径、编译选项和标志的一致性。只有确保这些细节正确无误,才能顺利地在Mingw环境下调用MSVC编译出的DLL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值