#include <iostream>
using namespace std;
int add (int a, int b);
float add (float a, float b);
int main()
{
int a = 6;
int b = 3;
float c = 5.46;
float d = 7.887987;
cout << a << "+" << b << "=" << add(a,b) << endl;
cout << c << "+" << d << "=" << add(c,d) << endl;
return 0;
}
int add(int a, int b)
{
return a+b;
}
float add(float a, float b)
{
return a+b;
}
Naming decoration aka Name mangling : Why?
In early days, C++ compilers used to convert C++ source into C code.
C does not support two functions with same names, hence names have to be decorated in order to conform them to 'C' identifier rules.
Even after development of compilers that can translate C++ source to machine code or assembly directly, system linkers did not support C++ symbols. So mangling still required.
The C++ language does not define a standard decoration scheme, so each compiler uses its own.
For following compiler mangling details are below:
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
For void add(int,int) Mangled name is _Z3addii
For void add(float,float) Mangled name is _Z3addff
In g++ generally,
All mangled symbols begin with _Z (note that an underscore followed by a capital is a reserved identifier in C and C++).
For nested names (including both namespaces and classes), this is followed by N. Then a series of
namespace Earth
{
class Man
{
void Born(float);
}
}
The name Born is mangled as
_ZN5Earth3Man4BornEf
_Z : mandatory
N : says that namespace/class follows
5Earth : 5 characters in following dentifier "Earth"
3Man : 3 characters in Man
4Born : 4 characters in Born
E : End of name
f : Parameter is float
No comments:
Post a Comment