C compiler links a function or symbol with appending under score before it. Function name main() links to symbol name _main(). Also if a variable name is int count; The linking symbol name will be _count.

For C++ compiler this is not true. C++ has function overloading mechanism. If C++ uses only under score before function name to link its symbol the purpose of overloading will not be resolved.

Take an example
Suppose in C++ we have add() function overloaded as
int add(int a, int b) - adds a and b, two arguments
int add(int a, int b, int c) - adds a, b and c, three arguments

Now if C++ uses _add() as symbol name then the two function body can never be linked.
To overcome this limitation of C, C++ linker uses named mangling.

Name mangling is the mechanism of C++ compiler to link functions and variables of same names to resolve them to some unique symbol names by altering the names with some extra information line index, argument size etc.
Function name mangling is done as <function index><function name>@<argument size>
Variable name mangling is done as ?<variable name>@@<UNIQUE ID>

Example will be
int add(int a, int b) links to

_1add@8 [function index 1, function name = add, argument size= sizeof(int)*2 = 8]

int add(int a, int b, int c) links to
_2add@12 [function index 2, function name = add, argument size= sizeof(int)*3 = 12]

int extrn_var links to
?extrn_var@@3HA [variable name = extrn_var, UNIQUE ID = 3HA ]

About our authors: Team EQA

You have viewed 1 page out of 27. Your DLL learning is 0.00% complete. Login to check your learning progress.