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();


cat cplusplus.cpp « EOF

#include "cplusplus.h"
int CPlusPlus::f(int in) { return in; }
int fglobal(int in) { return in; }


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


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 \
/OUT:"cplusplus.exe" /LIBPATH:"." main.obj libcplusplus.lib
cplusplus.exe *
# 15
ldd cplusplus.exe

Static lib with C++ method

rm main.obj
cl.exe /c /EHsc /nologo /DBUG main.cpp  link.exe \
/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 ;
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 ?

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…



nm.txt · Last modified: 2020/09/08 20:46 (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