mingw 生成.a 转为.lib

转:由MinGW编译出的.a生成VC下的.dll
(2013-12-01 15:42:11)
转载▼
广州市交运医院
    分类:C/C
使用MinGW编译一份库代码,可能是由于configure不好,导致只能生成.a文件,google发现一些方法,备份一下。
以下为源自blog.henix.info/blog/mingw-a-convert-dll.html的原文,感谢原作者:
最近遇到这样的问题:只有用 Cygwin 编译出来的静态库(.a),需要转换成可以在 VC++ 中调用 .dll 和 .lib 。
  网上大多说的是 dll 怎么生成 .lib 什么的,跟我要的不一样。所以我自己想了这么一种办法:
先用 ar 将 .a 中的所有 .o 文件解出来:
ar x libatlas.a
使用 MinGW 的 --export-all-symbols 选项,链接成 dll ,并导出所有符号:
gcc -shared -o atlas.dll *.o -Wl,--export-all-symbols,--output-def,atlas.def
使用 VC++ 自带的 lib 命令制作 .lib(需要上一步的 .def 文件):
lib /machine:i386 /def:atlas.def
Links:
/wiki/MSVC_and_MinGW_DLLs
以下为源自wwwblogs/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目录下到)
澳门科技大学2020年本科招生生成的dll文件和lib文件就可以被MSVC调用了。
注意:C++的dll不具有2进制级别的共享性,也就是说VC的MinGW的C++ dll不能混用。而C的dll却可以。
二、minGW调用MSVC生成的库
cl /LD testdll.c生成dll文件和lib文件
gcc - testmmain.c -ltestdll -L.编译连接生成可执行程序
PS:
e度作文网
以下为MinGW给的转换示例,备份一下。
原址:/wiki/MSVC_and_MinGW_DLLs
MSVC and MinGW DLLs
View
Revisions
Posted February 26th, 2009 by earnie
dll
faq
msvc文体用品与科技
TODO: Reformat to new wiki syntax.
!!! [Minimalist GNU for Windows | ]
!! MSVC and MinGW DLLs
Assume we have a testdll.h, testdll.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|wyw.dcweb/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 fi
rst 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.

本文发布于:2024-09-22 10:26:18,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/158894.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:文件   生成   编译   目录   科技   大学   备份
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议