将 Visual C 链接到 MinGW 的静态库

发布于 2024-11-26 00:42:55 字数 130 浏览 2 评论 0原文

如何将 Visual C++ (2010) 控制台应用程序与 MinGW 创建的 STATIC 库(*.a 格式)链接起来?它与 Visual C++ 2010 兼容吗?

谢谢。

How can one link Visual C++ (2010) console app with a STATIC library created by MinGW (*.a format)? Is it compatible with Visual C++ 2010?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔姬 2024-12-03 00:42:55

它不兼容。

但是,如果您从库中提取所有目标文件(使用 ar ),VC++ 链接器就能够处理这些文件(我测试了它,尽管我使用 cygwin gcc 而不是 mingw gcc)。请注意,如果您不使用 extern "C",您可能仍然会遇到名称修改问题。

当然,您可以使用VC++的LIB.EXE工具将它们制作成VC++格式的静态库。


正如 @Michael 指出的,如果您尝试在使用不同编译器构建的模块之间传递非 POD C++ 对象,那么您肯定会遇到问题。对此的修复与 DLL 情况相同:编写一个使用相同编译器(在本例中为 mingw)构建的包装器,该包装器公开可从其他工具链使用的 C 兼容接口。


// minimal.cpp
extern "C" int m(void) { return 7; }

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-cygwin/4.5.0/lto-wrapper.exe
Target: i686-pc-cygwin
Configured with: /gnu/gcc/releases/respins/4.5.0-1/gcc4-4.5.0-1/src/gcc-4.5.0/co
nfigure --srcdir=/gnu/gcc/releases/respins/4.5.0-1/gcc4-4.5.0-1/src/gcc-4.5.0 --
prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdi
r=/usr/lib --datadir=/usr/share --localstatedir=/var --sysconfdir=/etc --dataroo
tdir=/usr/share --docdir=/usr/share/doc/gcc4 --datadir=/usr/share --infodir=/usr
/share/info --mandir=/usr/share/man -v --with-gmp=/usr --with-mpfr=/usr --enable
-bootstrap --enable-version-specific-runtime-libs --libexecdir=/usr/lib --enable
-static --enable-shared --enable-shared-libgcc --disable-__cxa_atexit --with-gnu
-ld --with-gnu-as --with-dwarf2 --disable-sjlj-exceptions --enable-languages=ada
,c,c++,fortran,java,lto,objc,obj-c++ --enable-graphite --enable-lto --enable-jav
a-awt=gtk --disable-symvers --enable-libjava --program-suffix=-4 --enable-libgom
p --enable-libssp --enable-libada --enable-threads=posix --with-arch=i686 --with
-tune=generic --enable-libgcj-sublibs CC=gcc-4 CXX=g++-4 CC_FOR_TARGET=gcc-4 CXX
_FOR_TARGET=g++-4 GNATMAKE_FOR_TARGET=gnatmake GNATBIND_FOR_TARGET=gnatbind --wi
th-ecj-jar=/usr/share/java/ecj.jar
Thread model: posix
gcc version 4.5.0 (GCC)

$ gcc -c minimal.cpp

// minmain.cpp
extern "C" int m(void);

#include <iostream>

int main(void) { std::cout << m() << "\n"; }

R:\>cl /c /EHsc minmain.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

minmain.cpp

R:\>link minmain.obj minimal.o
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.


R:\>minmain
7

It's not compatible.

However, if you extract all the object files from the library (use ar), the VC++ linker is able to deal with those (I tested it, although I used cygwin gcc rather than mingw gcc). Note that you may still have name mangling problems if you don't use extern "C".

You may of course use VC++'s LIB.EXE tool to make these into a static library in VC++ format.


As @Michael points out, you will definitely have problems if you try to pass non-POD C++ objects between modules built with different compilers. The fix for this is the same as the DLL case: write a wrapper built with the same compiler (in this case mingw) that exposes a C-compatible interface usable from other toolchains.


// minimal.cpp
extern "C" int m(void) { return 7; }

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-cygwin/4.5.0/lto-wrapper.exe
Target: i686-pc-cygwin
Configured with: /gnu/gcc/releases/respins/4.5.0-1/gcc4-4.5.0-1/src/gcc-4.5.0/co
nfigure --srcdir=/gnu/gcc/releases/respins/4.5.0-1/gcc4-4.5.0-1/src/gcc-4.5.0 --
prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdi
r=/usr/lib --datadir=/usr/share --localstatedir=/var --sysconfdir=/etc --dataroo
tdir=/usr/share --docdir=/usr/share/doc/gcc4 --datadir=/usr/share --infodir=/usr
/share/info --mandir=/usr/share/man -v --with-gmp=/usr --with-mpfr=/usr --enable
-bootstrap --enable-version-specific-runtime-libs --libexecdir=/usr/lib --enable
-static --enable-shared --enable-shared-libgcc --disable-__cxa_atexit --with-gnu
-ld --with-gnu-as --with-dwarf2 --disable-sjlj-exceptions --enable-languages=ada
,c,c++,fortran,java,lto,objc,obj-c++ --enable-graphite --enable-lto --enable-jav
a-awt=gtk --disable-symvers --enable-libjava --program-suffix=-4 --enable-libgom
p --enable-libssp --enable-libada --enable-threads=posix --with-arch=i686 --with
-tune=generic --enable-libgcj-sublibs CC=gcc-4 CXX=g++-4 CC_FOR_TARGET=gcc-4 CXX
_FOR_TARGET=g++-4 GNATMAKE_FOR_TARGET=gnatmake GNATBIND_FOR_TARGET=gnatbind --wi
th-ecj-jar=/usr/share/java/ecj.jar
Thread model: posix
gcc version 4.5.0 (GCC)

$ gcc -c minimal.cpp

// minmain.cpp
extern "C" int m(void);

#include <iostream>

int main(void) { std::cout << m() << "\n"; }

R:\>cl /c /EHsc minmain.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

minmain.cpp

R:\>link minmain.obj minimal.o
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.


R:\>minmain
7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文