36

I'm just wondering why name mangling was never standardized by the C++ standard. Clearly having different name mangling algorithms hurts interoperability[1] and I don't see any advantage of having it implementation-defined.

That is, contrary to say calling conventions or size of primitives the machine itself doesn't care or even know how the function is called. So why wasn't it standardized and why is it still not standardized? Compilers have changed the rules in the past anyhow between versions.

[1] all those people exporting functions as extern "C" speak volumes.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Voo
  • 29,040
  • 11
  • 82
  • 156
  • 16
    Standardized name mangling only serves to give you a false sense of security, because you also have to standardize the ABI to get proper interoperability. And the C++ standards committee is unlikely to be in the ABI standardization business. – Raymond Chen Apr 12 '12 at 16:46
  • 3
    +1 @Raymond - lying to yourself about interoperability is bad news all around. It's just like relying on undefined behaviour. Sure, you *think* it works, but it really doesn't. – Carl Norum Apr 12 '12 at 16:47
  • True, agreeing on a C++ ABI would be much harder than for C. Slightly OT: What exactly in this question isn't considered a good fit for SO by 3 people? After all there quite clearly exists a good distinct answer as already shown. – Voo Apr 12 '12 at 17:00
  • There are two questions here: 1) Why isn't there *a* standard for name mangling and 2) Why isn't name mangling part of ISO C++ 14882 ? – MSalters Apr 13 '12 at 08:15
  • 1
    The ABI is implementation-related, that is, it strongly depends on the machine. On different machines you'd end up with different ABIs, which is not in the gestion of C++ as a language. It's more in the assembler's domain. That's why the ABI is not standardized by the C++ Committee. But it's often standardized by the implementors and machine designers, like Intel etc. Here's pretty detailed description of the Intel Itanium's x86 ABI for C++: http://sourcery.mentor.com/public/cxx-abi/ – SasQ Aug 03 '12 at 09:35
  • 2
    Same answer as all "Why is XXX not standardized": too hard, not useful enough, Microsoft wanted to prevent compatibility, etc. :-) – Ciro Santilli OurBigBook.com Jun 05 '15 at 13:22

2 Answers2

27

The standard does not address implementation details. There are many, many things which depend on the implementation, and which prevent programs from working together: how the classes are laid out, the structure of the vtable, etc. In general, compilers will change the name mangling if they change any of these. This is intentional, as it prevents code which would not work from linking.

It is possible for a given platform to define a C++ ABI; all compilers which adhere to it would use compatible implementations, and have a common name mangling. This is an issue for the platform vendors, however; for whatever reasons, very few vendors have defined a C++ ABI.

And the reason extern "C" works is because almost all platforms define a C ABI.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • 2
    It should be added that even if mangling was standardized, `extern "C"` would be needed anyway, because C does not do any mangling and C++ has to to support overloaded functions, so the symbols are different in any case. – Jan Hudec Apr 12 '12 at 16:53
  • 4
    @JanHudec It's not actually well defined what `extern "C"` means. What does it mean on a platform that doesn't have a C compiler, for example? In practice, however, most, if not all platforms define a C ABI, and `extern "C"` means to adhere to that. And here, too, name mangling isn't the only issue; I've worked on platforms where the order variables were pushed onto the stack was different between C and C++. (Also, most of the C compilers I've used do mangle names. Just in a much simpler manner than C++.) – James Kanze Apr 12 '12 at 17:00
23

The Standard doesn't actually require name mangling. For that matter, the Standard doesn't require IEEE floating point numbers, or any number of other things.

Until there was a widespread ABI it could rely on, GCC actually went out of its way to use a different name mangling scheme than its competitors:

G++ does not do name mangling in the same way as other C++ compilers. This means that object files compiled with one compiler cannot be used with another.

This effect is intentional, to protect you from more subtle problems. Compilers differ as to many internal details of C++ implementation, including: how class instances are laid out, how multiple inheritance is implemented, and how virtual function calls are handled. If the name encoding were made the same, your programs would link against libraries provided from other compilers--but the programs would then crash when run. Incompatible libraries are then detected at link time, rather than at run time.

Name mangling is also more complex than many programmers realize. For instance how would the Standard specify acceptable calling conventions for all platforms that C++ could run on? Should a RISC system be required to support x86 stdcall even though RISC systems usually pass their arguments in registers instead of on the stack?

cigien
  • 57,834
  • 11
  • 73
  • 112
Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
  • 1
    Actually, twos-complement has been standardized for integers as of C++20, so you should probably edit that example out of the answer. – cigien Feb 09 '21 at 07:07