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.

Sample code

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

Static lib with C++ method

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

Conclusion

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…

ToDo

MISC

nm.txt · Last modified: 2022/04/16 12:23 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki