By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,132 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,132 IT Pros & Developers. It's quick & easy.

name mangling of functions

P: n/a
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?

Dec 19 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
sa*****@yahoo.co.in wrote:
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.

--
Alan Johnson
Dec 19 '06 #2

P: n/a
>
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?

Dec 19 '06 #3

P: n/a
On Dec 19, 9:07 am, sam_...@yahoo.co.in wrote:
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?
Shouldn't be necessary, and I don't thing any compiler does that, but
you never know.

--
Erik Wikström

Dec 19 '06 #4

P: n/a
Le 19.12.2006 09:07, :
>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?
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).
Dec 19 '06 #5

P: n/a
Hi

Erik Wikström wrote:
On Dec 19, 9:07 am, sam_...@yahoo.co.in wrote:
>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.
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

Dec 19 '06 #6

P: n/a
sa*****@yahoo.co.in wrote:
>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?
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.

--
Alan Johnson
Dec 19 '06 #7

P: n/a
On 19 Dec 2006 00:59:21 -0800, "Erik Wikström"
<er****@student.chalmers.sewrote in comp.lang.c++:
On Dec 19, 9:07 am, sam_...@yahoo.co.in wrote:
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?

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 20 '06 #8

P: n/a
sa*****@yahoo.co.in wrote:
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.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 21 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.