Gregg wrote:
>
> "johny smith" <> wrote in
> news::
>
> > Can someone please explain to me why when linking a C with a C++
> > program that there has to be some flag to check to see if it is
> > compiled with C++.
>
> Unlike C compilers, C++ compilers need, when generating an object file,
> to "decorate" the symbols with type information in order to support
> overloading (using the same symbol for two or more functions with
> different parameter types). Since C does not support overloading like
> this, C compilers do not generate this decoration.
>
> The extern "C" tells the C++ compiler not to adorn symbol names with type
> information, but insted to store the symbol the way a C compiler would.
> Of course, functions declared this way will not support overloading, and
> must be non-member or static member functions.
>
Member functions cannot have C linkage, even if static (the compiler will
ignore the extern "C" {...} applied to them).
Interestingly, the standard avoids specifying what a language linkage
(either C++ or C) must or should entail, leaving it to the implementation.
So I suppose that instead of C++ name mangling we could have had some other
mechanism. I also read it to mean that, in principle, C and C++ linkages
are allowed to use different calling conventions. At any rate, it is
undefined behaviour to call a function through an expression with a different
language linkage; e.g., to use a pointer to a static member function where
a "C" function is expected.
(I have a vague idea of the real world implications of the last rule, but
I don't see a reason not to follow it).
Denis
|