Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a C++ code in gcc (g++) to see the name mangling on overloaded functions?

Tags:

c++

linux

gcc

I have overloaded functions like:

void f(int)
void f(int, int)
void f(int, float)

How to compile it, so that i can see the mangled output? Something like:

void f(int) should show: ?f@@YAXH@Z(int)

Like for example, to see pre-processor output we use -E, assembler output -s, what is it for name mangled output?

P.S: Platform is Linux

EDIT:

And by the answers here we go:

void func(int);
void func(int, int);
void func(void);
void func(char);

[root@localhost ~]# cat a.map | grep func
                0x0804881a                _Z4funcc
                0x08048790                _Z4funcv
                0x080487be                _Z4funcii
                0x080487ec                _Z4funci
like image 886
RajSanpui Avatar asked Jun 25 '13 16:06

RajSanpui


People also ask

Does C have name mangling?

Since C is a programming language that does not support name function overloading, it does no name mangling.

What is the name mangling and how does it work?

The need for name mangling arises where the language allows different entities to be named with the same identifier as long as they occupy a different namespace (typically defined by a module, class, or explicit namespace directive) or have different signatures (such as in function overloading).

What is name mangling technique?

Name mangling is the encoding of function and variable names into unique names so that linkers can separate common names in the language. Type names may also be mangled. Name mangling is commonly used to facilitate the overloading feature and visibility within different scopes.


2 Answers

For GCC try using:

-Xlinker -Map=output.map

http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

This will generate a map file which will have all of the mangled symbol names.

And for MSVC:

http://msdn.microsoft.com/en-us/library/k7xkk3e2(v=vs.80).aspx

This will generate something such as:

0002:00094190       ??0SerializationException@EM@@QAE@ABV01@@Z 10148190 f i y:foo.obj
like image 63
paulm Avatar answered Sep 28 '22 02:09

paulm


In Linux, I can see the names of all symbols via nm. For example:

$ nm a.out | grep pthread
                 w pthread_cancel@@GLIBC_2.2.5
                 U pthread_key_create@@GLIBC_2.2.5
                 U pthread_key_delete@@GLIBC_2.2.5
like image 30
chrisaycock Avatar answered Sep 28 '22 02:09

chrisaycock