Re: extern "C" ... again
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Thu, 24 Jul 2008 09:16:57 -0400
"Pelle" <pelle1911@xxxxxx> wrote in message
news:6e38e69d-7f84-4178-9b78-ce6364db3eac@xxxxxxxxxxxxxxxxxxxxxxxxxxx
First, let me try to summarize what I think I have learned so far:
There is no standard or convention on how and when to use name
mangling. It is up to the "producer" of compiler and linker to decide.
Even for C++, there is no rule how function overloading and other
stuff is to be achieved, though it is commonly achieved through name
mangling.
Furthermore, name mangling is not an invention that came along when
there started to be object oriented programming or even C++ as a more
specific example. It has been there with good old standard C in a more
simple form (using underscore, at-sign and numbers left and/or right
of the function name).
This "more simple form" is usually referred to as name decoration, to
distinguish it from C++-style name mangling.
I haven't yet found any statement on whether the old-style C name
mangling has any rule to it.
The documentation on calling conventions also describes name decoration.
See e.g.
http://msdn.microsoft.com/en-us/library/zkwh89ks.aspx
http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx
This is still compiler-specific - there's no standard saying names must
be decorated this way.
If I declare a function in a library as <extern "C">, it will revert
to old-style C name mangling. Though the <extern "C"> is not primarily
about name mangling, it is part of what it does (if the C++ compiler
does the more elaborate name mangling that is).
In practice, it _is_ primarily about name mangling.
However, if there is no rule to old-style C name mangling, how is the
linker to find the referred function?
Linker doesn't know nor care about name decoration or mangling rules.
All it has to do is to match a symbol in one .obj file to the same
symbol in another. It doesn't care whether the symbol is XYZ or _XYZ@12
or XYZ$ClassName$int (not a real example of name mangling). It's the
compiler's job to mangle or decorate the names in the same way in all
..obj files.
Imagine I use a 3rd party
library which has been built with a different compiler/linker
If you mean static library (.lib file), chances are high it won't work
with your compiler/linker, whether you use extern "C" or not. And not
only because of different name decoration rules. E.g. there's no
standard that dictates the precise binary layout of a struct: two
different compilers, seeing the same definition, may put the same field
at different offsets.
By the way, linker is not used when building a static library.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Follow-Ups:
- Re: extern "C" ... again
- From: Pelle
- Re: extern "C" ... again
- References:
- extern "C" ... again
- From: Pelle
- extern "C" ... again
- Prev by Date: Re: A few questions about XmlLite
- Next by Date: Re: extern "C" ... again
- Previous by thread: extern "C" ... again
- Next by thread: Re: extern "C" ... again
- Index(es):
Relevant Pages
|