Re: extern "C" ... again

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"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


.



Relevant Pages

  • Re: Fortran 90 modules suck for portability- why use them?
    ... it is clear that I am talking about name mangling. ... of the various flags gfortran has for name mangling; PathScale has the ... name mangling was perhaps inherited from g95 (the other OSS compiler) ...
    (comp.lang.fortran)
  • Re: Headers!
    ... >>anywhere an explanation of what are .h files and how to write them. ... Note that extern "C" isn't just about name mangling; ... One thing it doesn't do is to cause a C++ compiler to interpret ...
    (alt.comp.lang.learn.c-cpp)
  • Re: interface blocks
    ... Name mangling is part of life in the C++ world. ... every compiler on a given platform mangles ... with name mangling the error is not found until link time instead ... Personally I use the word linker when refering to the last step ...
    (comp.lang.fortran)
  • Re: Translating OO program to procedural program
    ... effort can be spreaded out to other langs, so I am working on to ... later by the procedural lang compiler. ... About C++, you should have a look at method name mangling, (google for ... of C), and CLOS (Common Lisp). ...
    (comp.compilers)
  • how to create a C++ fn. which can be called from both C and C++ files
    ... Now this fun writtenincpp() is being called from other C++ files in the ... extern "C" void writtenincpp(int,char) ... The compiler now does not perform name mangling and this can now be ...
    (comp.lang.c)