473,513 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

name mangling of functions

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
8 3130
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
>
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2513
by: Tim Slattery | last post by:
Does the C++ language standard mandate a particular name-mangling method? I'm trying to use the Entrust toolkit to create a C++ program that encrypts and decrypts files. Entrust supplies header files defining their objects and functions, and *.so files (I'm on a Sun Sparc machine running Solaris) containing the implementation of the...
6
6508
by: yyy | last post by:
my question is rather theoretical than practical in the pure programming aspect... is "name mangling" the same as "name decorating" ? browsing the web, you might find multiple people saying "yes", but i vaguely remember having read that it's not exactly like that one might say that "name mangling" is a part of c++ compiler specification...
3
5073
by: perfb | last post by:
have a C++ DLL that needs to be called be external non-C app that uses C interface, but cant figure out how to export C++ DLL functions without name mangling. if I declare _declspec(dllexport) void "C" foo(void) then it compiles ok, but I dont see any foo exported when dumpbin the dll
1
1896
by: noleander | last post by:
I need to use a library supplied by someone else: libjpeg.lib. This is a plain C library. I do not have the source code. I do have the header *.h files. When I run dumpbin on the libjpeg.lib, it contains symbols (entry points) like: jpeg_read_header I'm trying to link my program with this library. My program is in Visual C++. The...
6
1711
by: David Wade | last post by:
Folks, Does any one know of any "name mangling" software that allows standard C with long names to be linked? Any suggestions for a better place to look? I have tried putting various searchs into google to no avail. Dave.
5
1950
by: Subhransu Sahoo | last post by:
Hi All, Does the C++ standard tell function overloading can't be done with the return types of two functions ? If so, is it true that the name mangling scheme does not take the return type into account ?I know that C++ standard does not tell anything about name mangling and only talks bout function overloading. But, I have observed that the...
2
4832
by: developer.new | last post by:
Hi I have a question regarding this concept I learned about recently: Name Hiding. Here's what I've come across: There is a base class with two functions with the same name but different signature. A class inherits publicly from this base class and redefines one of the two functions in the derived class. In that case, a derived class...
4
1955
by: jason.cipriani | last post by:
Does anybody know if C++0x is going to change C++ name mangling at all? Such as, standardizing name mangling instead of leaving it up to the compiler? Jason
14
2976
by: Megalo | last post by:
why not make "name mangling" of C++ standard so should be possible to call the classes and the functions of C++ from other C++ compiler thanks
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.