The Secret Life of C++: Symbol Mangling

Symbol mangling is done by C++ because the linker only supports a global namespace. Basically, mangling is a lot like what C programmer do by hand when they name their functions things like libfoo_getobjattr(). Except that the C++ mangling is done automatically, and so it has to really be unique.

How names are mangled

Mangled names always start with _Z. Following this is the mangled name of either a function and its type, an object name, or a "special name".

There are a number of attributes that will be included in the mangled name of a function:

  1. Indication things are mangled (_Z)
  2. Nested name indication (Nnumber)
  3. Each component of the method name, including namespace, classes, and name, with a length and the identifier.
  4. Argument types
  5. Const indication (K)
  6. Reference indication (R)

How types are represented

Within the mangling there are a lot of types. Some have a short name, others get their full name.

TypeMangled Representation
voidv
inti
std::stringSs
std::ostreamSo
int Something::Inside::Deeper::deeperMethod(void)_ZN9Something6Inside6Deeper10deepMethodEv
int Something::Inside::Deeper::
deeperMethod(std::vector<std::string>)
__ZN9Something6Inside6Deeper10deepMethod
ENSt3__16vectorINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS7_IS9_EEEE

Name Mangling Example


        

        

        

        

        

More Information

For more detail, you can do worse than Wikipedia: Name Mangling in C++

Extern C

When you want to link with a C method, or be able to have a method called from C, you need to tag it as extern C. This tells the compiler not to mangle the symbol, so it will have a basic C looking symbol. For global variables there is no mangling and extern is redundant. But variables declared without extern are implicitly static. If they are initialized they will need symbols, and those symbols are mangled to avoid conflicting in the linker.


        

        

        

        

        
Fork me on GitHub