what is the use of extern "c"

Jonathan Wakely jwakely.gcc@gmail.com
Mon Jan 21 15:37:00 GMT 2013


On 21 January 2013 15:31, Jonathan Wakely wrote:
> On 21 January 2013 15:17, Tim Prince wrote:
>> On 1/20/2013 10:47 PM, horseriver wrote:
>>>
>>>
>>>     what is the use of extern c within a declaration?
>>>
>>>
>>>
>> extern "C" basically shuts off C++ symbol mangling (and facilities which
>> depend on it), so as to facilitate linking with C and with languages such as
>> Fortran which support C interoperability.
>
> And says to use C calling conventions, because there's no guarantee
> C++ uses the same ones (though with GCC it does.)
>
>>  The definition seems rather
>> loose; we frequently see reference operators used even though their
>> compatibility with C is doubtful.
>
> That's perfectly well-defined. A C program would be able to link to
> such a function and e.g. take its address. It just won't be able to
> call it (correctly) because it can't pass it a reference.

Hmm, actually it couldn't even declare it correctly in the C code, so
couldn't refer to it at all.

But that doesn't mean you can't have extern "C" functions with
reference parameters, it just means they're not very useful!



More information about the Gcc-help mailing list