8

I have a bit of code which has the following line

  #pragma comment(linker, "/include:_test@12") 

The project which uses this code works fine when I compile the code using C++ Visual Studio 2010 with configuration type 32bit (I am also on a 32 bit windows machine).

I get a link error when I change the machine to 64bit and use x64 configuration which compiling with C++ Visual Studio 2010.

Is C++ name mangling different for 32bit vs 64bit? If so, where can I find the 64bit C++ name mangling conventions?

Mat
  • 202,337
  • 40
  • 393
  • 406
user1612986
  • 1,373
  • 3
  • 22
  • 38

1 Answers1

10

Yes the name mangling is different between 32 and 64 bit. A reasonable article covering the exact formats can be found here. You can tell the major differences pretty quickly, however, by simply compiling to both targets and examining the resulting map files. From my experience they're almost identical (64bit adds a small datum, potentially changes others).

Simple sample: void foo();

32bit: ?foo@A@@QAEXXZ
64bit: ?foo@A@@QEAAXXZ

For non-mangled std call, the length suffix can be substantially different, depending on the parameter stack usage. The default 64-bit settings for VC++ do not prepend underscores nor does it encode length-suffixes. The following was compiled both 32/64bit configs with pure out-of-the-box settings:

extern "C" int _stdcall func2(int, int, char*);

32bit: _func2@12
64bit: func2

Not much point there, is there.

Completing the circuit, unmangled _cdecl, which does this:

extern "C" int _cdecl func2(int, int, char*);

32bit: _func2
64bit: func2

If it seems like they went out of their way to make you know what you're pulling-in or exporting-out, evidence suggests you're probably correct.

Nulano
  • 1,148
  • 13
  • 27
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • It may be quite different actually... If `test` receives three pointer parameters then it will change from `12` to `24`... Ah, also this is not a C++ function. It seems to be an `stdcall` C function. – Yakov Galka Nov 14 '12 at 19:43
  • where can i learn about this. i am new to these concepts. also i think mine is a stdcall and the example you gave is for cdecl. i have couple of other calls like #pragma comment(linker, "/export:test@0=test") how should i change those. a reference clarifying these will be great. – user1612986 Nov 14 '12 at 19:52
  • There are a host of documents on C++ name mangling (I linked you one of them). You appear to be using extern "C" _stdcall for your exports, which, as demonstrated above, are different in 32.vs.64-bit. There are articles literally all over MS and Wiki. Though VB-related, I like [this post](http://msdn.microsoft.com/en-us/library/dt232c9t(v=vs.71).aspx) as it does a decent job of talking about it. One way or another, though, you're going to have to clean up your #pragmas to #ifdef WIN64 and either import or export the appropriate names, I'm afraid. – WhozCraig Nov 14 '12 at 20:02
  • From what I can tell, the only calling conventions available for 64-bit systems are `vectorcall` and the default x64 calling convention( which seems to be a version of `fastcall`, from what I can find online). So, `__cdecl`, `__stdcall`, `__fastcall`, and `__thiscall` are all ignored when compiling as 64-bit. – Justin Time - Reinstate Monica Nov 03 '16 at 18:05
  • The Wikipedia page has been migrated to: https://en.wikiversity.org/wiki/Visual_C%2B%2B_name_mangling – Nulano Jul 15 '20 at 21:58