Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i avoid name mangling?

Tags:

c++

How can I avoid name mangling in C++?

like image 783
n00ki3 Avatar asked Feb 07 '09 21:02

n00ki3


People also ask

Why name mangling is required?

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 are the ways to stop name mangling in C++ Mcq?

First, in your source code prepend extern "C" to get rid of C++ mangling; this still leaves C mangling though. Second, use the -Wl,--kill-at command line option when compiling.

Does C use name mangling?

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

What is name mangling in Java?

Name mangling is a term that denotes the process of mapping a name that is valid in a particular programming language to a name that is valid in the CORBA Interface Definition Language (IDL).


2 Answers

You can't. It's built into compilers to allow you overloading functions and to have functions with the same name in different classes and such stuff. But you can write functions that are mangled like C functions. Those can be called from C code. But those can't be overloaded and can't be called by "normal" C++ function pointers:

extern "C" void foo() {

}

The above function will be mangled like C functions for your compiler. That may include no change at all to the name, or some changes like a leading "_" in front of it or so.

like image 122
Johannes Schaub - litb Avatar answered Oct 05 '22 06:10

Johannes Schaub - litb


Other way:

Controlling Names Used in Assembler Code (gcc spec.)

You can specify the name to be used in the assembler code for a C function or variable by writing the asm (or __asm__) keyword after the declarator. It is up to you to make sure that the assembler names you choose do not conflict with any other assembler symbols, or reference registers.

To specify the assembler name for functions, write a declaration for the function before its definition and put asm there, like this:

 int func () asm ("MYFUNC");

 int func ()
 {

g++ will compile it and nm -D output will be

0000000000001e02 T MYFUNC

instead of

0000000000001e02 T _Z4funcv

Tested on g++ 4.9.2

like image 28
befzz Avatar answered Oct 05 '22 06:10

befzz