Link Visual C againts MinGW's static library
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.
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
精彩评论