Why is the C++ mangling on Linux not unique

Please consider the following C++ program:

int _Z5func2v;

void func2() {

When you try to compile it, it fails:

$ g++ test.cpp -c
/tmp/cc1RDxpU.s: Assembler messages:
/tmp/cc1RDxpU.s:13: Error: symbol `_Z5func2v' is already defined
/tmp/cc1RDxpU.s: Error: .size expression for _Z5func2v does not evaluate to a constant

This is because the program defines a global variable that has the same name as the function gets after name mangling.

I can think of many ways this would be resolved, the simplest of which is to use two underscores at the beginning of the mangled name (two underscores are reserved for private implementation use).

The question is: why was a scheme chosen that makes it possible?

1 answer

  • answered 2019-01-11 05:14 David Schwartz

    Names that begin with a single underscore followed by a capital letter are reserved the same way names containing two underscores are.

    For example, from the 2003 C++ standard, section, "Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use."