Sunday 21 April 2013

Name Mangling

As it is possible to overload functions (e.g. in the example above there are three print
functions) and as it is possible to use C++ with your existing linker, there must be some
mechanism to generate a unique name for each overloaded function. Each function's name,
number and type of parameters are combined to produce a unique name. This process is
called name mangling. Occasionally name mangling is not required, one example is linking
with C code. To turn the mangling off, a linkage specification can be used when declaring
the C routines that will be used:
extern "C" void fred(int);
fred(int) will now no longer be mangled. If the linkage specification is not used, and
fred is declared as void fred(int) an error such as Undefined: fred_Fi will be
produced when linking. fred_Fi is the mangled name of void fred(int)
When a group of C functions need to be declared, they can be placed in a linkage
specification block:
extern "C" {
int printf(char *fmt ...);
int scanf(char *fmt ...);
}

No comments:

Post a Comment