Program Linkage Between Identifiers

The association, or lack of association, between two identical identifiers is known as linkage. The kind of linkage that an identifier has depends on the way that it is declared.

Types of identifier linkage and their descriptions are:

Internal Linkage Internal linkage occurs where identical identifiers within a single source file refer to the same data object or function.

The following kinds of labels have internal linkage:

  • All identifiers with file or block scope that have the keyword static in their declarations. Functions with static storage class are visible only in the source file in which you define them.
  • Functions qualified with _Inline and C++ inline functions.
  • All identifiers declared at file scope with the specifier const and not explicitly declared extern.

A variable that has static storage class can be defined within a block or outside of a function.

  • If the definition occurs within the block, the variable has internal linkage and is visible only within the block after its declaration is seen.
  • If the definition occurs outside of a function, the variable has internal linkage, and is available from the point where it is defined until the end of the current source file.

A class name that has no static members or non-inline member functions, and that has not been used in the declaration of an object, function, or class, is local to its translation unit.

If the declaration of an identifier has the keyword extern, and if a previous declaration of the identifier is visible at file scope, the identifier has the same linkage as the first declaration.

External Linkage External linkage occurs where identical identifiers in separately compiled files refer to the same data object or function.

The following kinds of identifiers have external linkage:

  • Identifiers with file or block scope that have the keyword extern in their declarations.
If a previous declaration of the identifier is visible at file scope, the identifier has the same linkage as the first declaration. For example, a variable or function that is first declared with the keyword static and is later declared with the keyword extern has internal linkage.
  • Function identifiers declared without storage-class specifiers.
  • Object identifiers that have file scope declared without a storage-class specified. Storage is allocated for such object identifiers.
  • Static class members and non-inline member functions.

Identifiers declared with the keyword extern can be defined in other translation units.

No Linkage No linkage occurs where each identical identifier refers to a unique object.

The following kinds of identifiers have no linkage:

  • Identifiers that do not represent an object or a function, including labels, enumerators, typedef names, type names, and template names.
  • Identifiers that represent a function argument.
  • Identifiers declared inside a block without the keyword extern.

 



Scope of Indentifier Visibility


Linkage Specifications - Linking to non-C++ Programs