Linkage Specifiers

On occasions it is required to interface with code written in C or assembly language. Functions written in C differ from those written in C++ in that their names are not mangled by the compiler. The syntax available to indicate the presence of C or assembly language routines is the subject of this section. Note that reference is made to the syntax of linkage specifications in the section on declarations.


      extern string_literal { declaration_list }
      extern string_literal declaration
       declaration_list declaration


The string literal specifies the type of linkage that is applied to the declarations. The interpretation placed upon the string literal is implementation dependent; however, two values for which support is mandatory are:

"C"   declarations are for the C programming language,
"C++" declarations are for the C++ programming language.

The following applies to linkage specifications.


extern "C" int cf(int,int);  // cf has "C" linkage
extern "C" int cf();         // Syntax error, can't have different function with "C" linkage
extern "C"
 int f(int,int);                    // This one is "C"
 extern "C++" int g(double,double); // And this one is "C++"
 int h(int,int);                    // And this one is "C"
int f(int i, int j) {return i+j;}   // Correct to redeclare or define without linkage specifiers

Implementation Notes

Apart from the fact that the names of "C" functions are not mangled, there may be other considerations that apply to such functions. For example, the calling conventions of "C" functions may differ from those of "C++" functions. That is, the order in which parameters are pushed onto the stack and whether the caller or callee is responsible for removing parameters from the stack may differ between "C" and "C++" functions. The calling conventions of C++ functions may vary amongst C++ compilers intended to run under the same operating system. Name mangling schemes may also vary between implementations of the C++ language. Each compiler may have special type specifiers that apply to functions or classes (or both). For example, _export may be used to indicate that a function (or possibly all functions within a class) are to be exported.