C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

name mangling of functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Tue Dec 19, 2006 10:10 am    Post subject: name mangling of functions Reply with quote



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?
Back to top
Alan Johnson
Guest





PostPosted: Tue Dec 19, 2006 10:10 am    Post subject: Re: name mangling of functions Reply with quote



sam_cit (AT) yahoo (DOT) co.in wrote:
Quote:
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
Back to top
Guest






PostPosted: Tue Dec 19, 2006 10:10 am    Post subject: Re: name mangling of functions Reply with quote



Quote:

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?
Back to top
Erik Wikström
Guest





PostPosted: Tue Dec 19, 2006 10:10 am    Post subject: Re: name mangling of functions Reply with quote

On Dec 19, 9:07 am, sam_...@yahoo.co.in wrote:
Quote:
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
Back to top
Serge Paccalin
Guest





PostPosted: Tue Dec 19, 2006 10:10 am    Post subject: Re: name mangling of functions Reply with quote

Le 19.12.2006 09:07, :

Quote:
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.

Quote:
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).
Back to top
Markus Moll
Guest





PostPosted: Tue Dec 19, 2006 10:10 am    Post subject: Re: name mangling of functions Reply with quote

Hi

Erik Wikström wrote:

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.