In C++, this indicates that a function uses the C calling convention, et cetera, rather than those of C++.

Can be used like this:
extern "C" {
   void f( int somearg );
   void g( int somearg, char *somestring );
}

or:
extern "C" void f( int somearg );
extern "C" void g( int somearg, char *somestring );

Often #ifdef __cplusplus is done in a header file to do an extern "C" if the compiler is a C++ one.

Even more importantly than specifying a calling convention (after all, C and C++ generally share the same calling convention, unlike (say) C and Pascal), declaring extern "C" tells the compiler to use C linkage for this name. Typically, in an effort to use the vendor's standard (but wholly inappropriate) linker, a C++ compiler will perform "name mangling" on function names to encode type information in the name (recall that C++ lets you overload function names!). C doesn't mangle names. extern "C" typically tells the compiler to emit unmangled identifiers, so they will match those emitted by a C compiler.

Other linkage conventions may be given, but none are standard. Typically, Fortran will have nearly the same linkage as C. Having exactly the same conventions would make cross-language linking less interesting...

extern "C" also tells the C++ compiler that the function should be called using the appropriate calling convention (ABI) for C (rather than for C++). I doubt the platform exists which has different calling conventions, but it's there in the standard. About the only thing I can imagine is that on a segmented architecture, C++ and C might have different sizes for the void * pointer -- this pointer is required to be large enough to hold any other pointer, and some C++ pointers to objects (especially ones with virtual functions) might require more space.

Log in or register to write something here or to contact authors.