由MinGW编译出的.a生成VC下的.dll

使用MinGW编译一份库代码,可能是由于configure不好,导致只能生成.a文件,google发现一些方法,备份一下。

 

以下为源自http://blog.henix.info/blog/mingw-a-convert-dll.html的原文,感谢原作者:

最近遇到这样的问题:只有用 Cygwin 编译出来的静态库(.a),需要转换成可以在 VC++ 中调用 .dll 和 .lib 。

  网上大多说的是 dll 怎么生成 .lib 什么的,跟我要的不一样。所以我自己想了这么一种办法:

  1. 先用 ar 将 .a 中的所有 .o 文件解出来:

    ar x libatlas.a
  2. 使用 MinGW 的 --export-all-symbols 选项,链接成 dll ,并导出所有符号:

    gcc -shared -o atlas.dll *.o -Wl,--export-all-symbols,--output-def,atlas.def
  3. 使用 VC++ 自带的 lib 命令制作 .lib(需要上一步的 .def 文件):

    lib /machine:i386 /def:atlas.def

Links:

 

以下为源自http://www.cnblogs.com/lovesaber/archive/2012/11/27/2790297.html

一、MSVC调用MINGW生成的dll和lib

gcc -shared -o testdll.dll testdll.c-Wl,--output-def,testdll.def 生成dll文件和def文件

lib /machine:i386 /def:testdll.def 生成lib 文件和exp文件。(lib为MSVC的工具,可以在VS安装的bin目录下找到)

生成的dll文件和lib文件就可以被MSVC调用了。

注意:C++的dll不具有2进制级别的共享性,也就是说VC的MinGW的C++ dll不能混用。而C的dll却可以。

二、minGW调用MSVC生成的库

cl /LD testdll.c 生成dll文件和lib文件

gcc -o testmain.exe testmmain.c -ltestdll -L. 编译连接生成可执行程序

 

 

PS:

以下为MinGW官网给的转换示例,备份一下。

原址:http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs

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 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. 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 from Anders Norlander (since his web site is no longer available, you may choose to download
[this version|http://wyw.dcweb.cn/download.asp?path=&file=reimp_new.zip]
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 included 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.

转载于:https://my.oschina.net/jjyuangu/blog/1517500

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值