9 Declarations [dcl.dcl]

9.11 Linkage specifications [dcl.link]

All functions and variables whose names have external linkage and all function types have a language linkage.
[Note 1:
Some of the properties associated with an entity with language linkage are specific to each implementation and are not described here.
For example, a particular language linkage might be associated with a particular form of representing names of objects and functions with external linkage, or with a particular calling convention, etc.
— end note]
The default language linkage of all function types, functions, and variables is C++ language linkage.
Two function types with different language linkages are distinct types even if they are otherwise identical.
Linkage between C++ and non-C++ code fragments can be achieved using a linkage-specification:
The string-literal indicates the required language linkage.
This document specifies the semantics for the string-literals "C" and "C++".
Use of a string-literal other than "C" or "C++" is conditionally-supported, with implementation-defined semantics.
[Note 2:
Therefore, a linkage-specification with a string-literal that is unknown to the implementation requires a diagnostic.
— end note]
Recommended practice: The spelling of the string-literal should be taken from the document defining that language.
For example, Ada (not ADA) and Fortran or FORTRAN, depending on the vintage.
Every implementation shall provide for linkage to the C programming language, "C", and C++, "C++".
[Example 1: complex sqrt(complex); // C++ language linkage by default extern "C" { double sqrt(double); // C language linkage } — end example]
A module-import-declaration shall not be directly contained in a linkage-specification.
A module-import-declaration appearing in a linkage specification with other than C++ language linkage is conditionally-supported with implementation-defined semantics.
Linkage specifications nest.
When linkage specifications nest, the innermost one determines the language linkage.
[Note 3:
A linkage specification does not establish a scope.
— end note]
A linkage-specification shall inhabit a namespace scope.
In a linkage-specification, the specified language linkage applies to the function types of all function declarators and to all functions and variables.
[Example 2: extern "C" // f1 and its function type have C language linkage; void f1(void(*pf)(int)); // pf is a pointer to a C function extern "C" typedef void FUNC(); FUNC f2; // f2 has C++ language linkage and // its type has C language linkage extern "C" FUNC f3; // f3 and its type have C language linkage void (*pf2)(FUNC*); // the variable pf2 has C++ language linkage; its type // is “pointer to C++ function that takes one parameter of type // pointer to C function'' extern "C" { static void f4(); // the name of the function f4 has internal linkage, // so f4 has no language linkage; its type has C language linkage } extern "C" void f5() { extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage) // obtained from previous declaration. } extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage) // obtained from previous declaration. void f6() { extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage) // obtained from previous declaration. } — end example]
A C language linkage is ignored in determining the language linkage of class members, friend functions with a trailing requires-clause, and the function type of class member functions.
[Example 3: extern "C" typedef void FUNC_c(); class C { void mf1(FUNC_c*); // the function mf1 and its type have C++ language linkage; // the parameter has type “pointer to C function'' FUNC_c mf2; // the function mf2 and its type have C++ language linkage static FUNC_c* q; // the data member q has C++ language linkage; // its type is “pointer to C function'' }; extern "C" { class X { void mf(); // the function mf and its type have C++ language linkage void mf2(void(*)()); // the function mf2 has C++ language linkage; // the parameter has type “pointer to C function'' }; } — end example]
If two declarations of an entity give it different language linkages, the program is ill-formed; no diagnostic is required if neither declaration is reachable from the other.
A redeclaration of an entity without a linkage specification inherits the language linkage of the entity and (if applicable) its type.
Two declarations declare the same entity if they (re)introduce the same name, one declares a function or variable with C language linkage, and the other declares such an entity or declares a variable that belongs to the global scope.
[Example 4: int x; namespace A { extern "C" int f(); extern "C" int g() { return 1; } extern "C" int h(); extern "C" int x(); // error: same name as global-space object x } namespace B { extern "C" int f(); // A​::​f and B​::​f refer to the same function extern "C" int g() { return 1; } // error: the function g with C language linkage has two definitions } int A::f() { return 98; } // definition for the function f with C language linkage extern "C" int h() { return 97; } // definition for the function h with C language linkage // A​::​h and ​::​h refer to the same function — end example]
A declaration directly contained in a linkage-specification is treated as if it contains the extern specifier ([dcl.stc]) for the purpose of determining the linkage of the declared name and whether it is a definition.
Such a declaration shall not specify a storage class.
[Example 5: extern "C" double f(); static double f(); // error extern "C" int i; // declaration extern "C" { int i; // definition } extern "C" static void g(); // error — end example]
[Note 4:
Because the language linkage is part of a function type, when indirecting through a pointer to C function, the function to which the resulting lvalue refers is considered a C function.
— end note]
Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent.
Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved.