Why compilers C ++ "mangle" function names.
- 2so that the linker could then distinguish functions of the same name with different parameters. the linker already knows nothing about the parameters - Mike
- "Mangle"? I do not understand what it is :) - Michael Vaysman
- one@MichaelVaysman mangle - αλεχολυτ
- oneahh :)) thanks! en.wikipedia.org/wiki/Name_mangling - Michael Vaysman
1 answer
See, C ++ compilers inherited a linker from C.
In the C-shnom linker, the name of the function was directly mapped to the name of the object. This worked because there are no overloaded functions in C.
But in C ++, functions are overloaded (as well as namespaces and classes), and functions
int f(int x) { ... } and
int f(int x, int y) { ... } would get the same name! And the linker did not know how to handle such a situation, for him all the names had to be unique. So that the two functions written above could be put together, and in order not to change the linker's logic, they came up with a simple way out: encode the function signature, that is, the types of parameters (as well as the class name and namespace) in the name, transfer the augmented name to the linker.
In principle, such a modification of the names is not mandatory, and is not required by the standard. Nevertheless, this is a popular implementation technique of the compiler and linker interaction, which is used in both the POSIX standard and Windows ABI.
- It turns out you can not prevent distorting the names of overloaded functions? - Stanislav Petrov
- 2@StanislavPetrov: No, you can not, it does not slink. - VladD