Related : C CPlusPlus GCC Cygwin MSVC
Mixing compiled objects between GCC and MSVC does work at least when shared code is in C, C++ is causing trouble on different name mangeling.
cat cplusplus.h « EOF
#ifndef CPlusPlus_h_ #define CPlusPlus_h_
#define API
class API CPlusPlus { public: static int f(int in); }; extern "C" { API int fglobal(int in); API CPlusPlus* getInstanceCPlusPlus(); } #endif
EOF
cat cplusplus.cpp « EOF
#include "cplusplus.h" int CPlusPlus::f(int in) { return in; } int fglobal(int in) { return in; }
EOF
cat main.cpp « EOF
#include <cstdlib> #include <iostream> using namespace std; #include "cplusplus.h" int main(int argc,char* argv[[]]) { cout << "# " <<fglobal(argc)<< endl; // C link : works : 00000008 T _fglobal CPlusPlus * p = 0; p = new CPlusPlus; // C++ link : works #ifdef BUG // C++ link : fails : ==== name mangeling between gcc and msvc=== cout << p->f(argc) << endl; // printf("# %d\n", getInstanceCppModule()->f(argc) ); #endif // @see http://cygwin.com/ml/cygwin/2005-03/msg00413.html return EXIT_SUCCESS; }
EOF
g++ -c -o ./cplusplus.o -mno-cygwin -mwindows -g -I. -Dcygwin -D_GNU_SOURCE -I. ./cplusplus.cpp ar rcu ./libcplusplus.lib cplusplus.o
cl.exe /c /EHsc /nologo main.cpp
link.exe \ /INCREMENTAL /NOLOGO /MACHINE:X86 /DEBUG /SUBSYSTEM:CONSOLE \ /OUT:"cplusplus.exe" /LIBPATH:"." main.obj libcplusplus.lib
cplusplus.exe * # 15
ldd cplusplus.exe h:/src/cplusplus/cplusplus.exe
C:\WINDOWS\System32\KERNEL32.dll
C:\WINDOWS\System32\ntdll.dll
rm main.obj cl.exe /c /EHsc /nologo /DBUG main.cpp
cc-msvc.sh link.exe \ /INCREMENTAL /NOLOGO /MACHINE:X86 /DEBUG /SUBSYSTEM:CONSOLE \ /OUT:"cplusplus.exe" /LIBPATH:"." main.obj libcplusplus.lib main.obj : error LNK2019: unresolved external symbol "public: static int __cdecl CPlusPlus::f(int)" (?f@CPlusPlus@@SAHH@Z) referenced in function _main
cplusplus.exe : | error LNK1120: 1 unresolved externals
nm libcplusplus.lib ; cplusplus.o: 00000000 b .bss 00000000 d .data 00000000 N .stab 00000000 N .stabstr 00000000 t .text 00000000 T __ZN9CPlusPlus1fEi 00000008 T _fglobal
For obscure reasons, the mangling name are different
So Why it is impossible to link C++ GCC's objects coming with MSVC ?
http://cygwin.com/ml/cygwin/2005-03/msg00413.html
Can dynamic libs workaround this ?
I doubt GCC can write a compatible MSVC “library stub” ( XYZ.lib that goes along XYZ.dll, not a static one : libXYZ.lib )
But maybe runtime linking is possible … I am investigating on this…
Thanks go to : Arend-Jan Westhoff, Christopher Faylor, Jonathan Arnold, Tim Prince and all the Cygwin pple.
Some additional info:
<http://www.network-theory.co.uk/docs/gccintro/gccintro_65.html> <http://www.network-theory.co.uk/docs/gccintro/gccintro_16.html> <http://www.linuxquestions.org/questions/archive/9/2003/10/3/105795> <http://www.linuxlookup.com/HOWTO/GCC-HOWTO/x575.html#AEN579>
<http://www.cygwin.com/faq/faq_3.html#SEC103> <http://www.cygwin.com/faq/faq_3.html#SEC102>