name mangling of functions

S

sam_cit

Hi Everyone,

I read somwhere that c++ compiler does name mangling of functions
which is why c source code can't invoke functions from object files
that were generated using c++ compiler. Can anyone tell in detail as to
what name mangling is all about?
 
A

Alan Johnson

Hi Everyone,

I read somwhere that c++ compiler does name mangling of functions
which is why c source code can't invoke functions from object files
that were generated using c++ compiler. Can anyone tell in detail as to
what name mangling is all about?

In C, functions all exist in the global namespace. You can have only
one function with a particular name throughout your entire program. In
C++, you may have indefinitely many functions with the same name, so
long as each is in a different namespace, class, etc, or even in the
same namespace if they differ in the number or type of arguments.

Clearly, its name alone is no longer sufficient for a linker to uniquely
identify a function, so some C++ compilers solve this problem by giving
functions that have the same name in the source code some mangled name
in the object file. This mangled name might take into account the
class, namespace, and number and type of arguments the function has so
that each function with the same name will have a different mangled name.

Note, however, that how this is done (or whether it is done at all) is
an implementation detail. The standard doesn't say anything about name
mangling.
 
S

sam_cit

In C, functions all exist in the global namespace. You can have only
one function with a particular name throughout your entire program. In
C++, you may have indefinitely many functions with the same name, so
long as each is in a different namespace, class, etc, or even in the
same namespace if they differ in the number or type of arguments.

Clearly, its name alone is no longer sufficient for a linker to uniquely
identify a function, so some C++ compilers solve this problem by giving
functions that have the same name in the source code some mangled name
in the object file. This mangled name might take into account the
class, namespace, and number and type of arguments the function has so
that each function with the same name will have a different mangled name.

Does it mean name mangling is also done for variables? and if i have
only one function definition for a particular function name, will the
compiler perform name mangling?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Does it mean name mangling is also done for variables? and if i have
only one function definition for a particular function name, will the
compiler perform name mangling?

Shouldn't be necessary, and I don't thing any compiler does that, but
you never know.
 
S

Serge Paccalin

Le 19.12.2006 09:07, :
Does it mean name mangling is also done for variables?

You might have several variables with the same name:
- in different namespaces
- as data members in different classes
so the compiler needs at least put these in the symbol names.
and if i have
only one function definition for a particular function name, will the
compiler perform name mangling?

The compiler cannot know that, since it compiles one module at a time.
Only the linker could know a function name is used for one function.

--
___________
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Pour bien répondre avec Google, ne pas cliquer
-'(__) « Répondre », mais « Afficher les options »,
_/___(_) puis cliquer « Répondre » (parmi les options).
 
M

Markus Moll

Hi
Shouldn't be necessary, and I don't thing any compiler does that, but
you never know.

Uhm...
$ cat t.cc
int func(double x) { return 42; }
$ g++ -o t.o -c t.cc
$ nm t.o
U __gxx_personality_v0
00000000 T _Z4funcd

"_Z4funcd" looks mangled to me...
A compiler can't know if lateron you will link with some other file where
something with the same name is defined again.

And at least GCC also does not care, even if it knows (int main() { return
func(0.0); } added to the program):
$ g++ -fwhole-program -o t.o -c t.cc
$ nm t.o
U __gxx_personality_v0
0000001a T main
00000000 t _Z4funcd

Markus
 
A

Alan Johnson

Does it mean name mangling is also done for variables? and if i have
only one function definition for a particular function name, will the
compiler perform name mangling?

Yes, it can apply to variables as well. As to whether or not the
compiler will mangle a name in any given situation, it depends entirely
on the compiler. If you need to link with C, declare the functions as
extern "C". Example:

extern "C"
{
void foo() ;
void bar() ;
}


or alternatively:

extern "C" void foo() ;
extern "C" void bar() ;

This should cause the compiler to produce functions with C linkage.
 
J

Jack Klein

Shouldn't be necessary, and I don't thing any compiler does that, but
you never know.

Just a little bit of thought will tell you that what you "thing" must
be wrong.

The C++ language requires that you be able to translate (compile)
separate source files (technically "translation units") separately and
at different times, and then put the results together into a single
executable.

So if you compile a source file containing this function:

int func(int i) { return i + 3 }

....the compiler has no way of knowing whether it might wind up in the
same program as another source file containing this function:

double func(double d) { return d / 3.0 }

So if a C++ compiler needs some mechanism to uniquely identify
function names, it must do so even when it only sees one definition
for the function in a source file.
 
E

E. Robert Tisdale

I read somwhere that [the] C++ compiler does name mangling of functions
which is why C source code can't invoke functions from object files
that were generated using c++ compiler.
Can anyone tell in detail as to what name mangling is all about?

Name mangling is not necessary in C++.
The fully qualified function prototype

func(int)

or

func(double)

is sufficient to identify the the function.
The problem is that the typical assemblers and link editors
that C++ compilers call to create an executable program
don't recognize function prototypes as valid symbols
so the C++ compiler "mangles" the function prototype
to create symbols that the assembler and link editor will accept.
The C++ computer programming language does not specify
how function prototypes are mangled -- it doesn't need to.
Name mangling schemes are implementation dependent --
they vary from one C++ compiler to another
and from one version to the next.

You can use a utility to extract the symbol table from an object file
(UNIX programmers use nm) and use that symbol to call a C++ function
from a C program but the linkage will probably fail
if you change or upgrade the C++ compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top