Linkage can make an identifier declared in different scopes or declared multiple times within the same scope refer to the same object or function. Identifiers are classified as externally linked, internally linked, or not linked. These three kinds of linkage have the following characteristics [Kirch-Prinz 2002]:
- External linkage: An identifier with external linkage represents the same object or function throughout the entire program, that is, in all compilation units and libraries belonging to the program. The identifier is available to the linker. When a second declaration of the same identifier with external linkage occurs, the linker associates the identifier with the same object or function.
- Internal linkage: An identifier with internal linkage represents the same object or function within a given translation unit. The linker has no information about identifiers with internal linkage. Consequently, these identifiers are internal to the translation unit.
- No linkage: If an identifier has no linkage, then any further declaration using the identifier declares something new, such as a new variable or a new type.
According to the C Standard, 6.2.2 [ISO/IEC 9899:2011], linkage is determined as follows:
If the declaration of a file scope identifier for an object or a function contains the storage class specifier
static, the identifier has internal linkage.
For an identifier declared with the storage-class specifier
externin a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier
extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.
The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier
Use of an identifier (within one translation unit) classified as both internally and externally linked is undefined behavior. (See also undefined behavior 8.) A translation unit includes the source file together with its headers and all source files included via the preprocessing directive
The following table identifies the linkage assigned to an object that is declared twice in a single translation unit. The column designates the first declaration, and the row designates the redeclaration.
Noncompliant Code Example
In this noncompliant code example,
i5 are defined as having both internal and external linkage. Future use of either identifier results in undefined behavior.
Microsoft Visual Studio 2013 issues no warnings about this code, even at the highest diagnostic levels.
GCC and Clang 14 both generate fatal diagnostics for the conflicting definitions of
This compliant solution does not include conflicting definitions:
Use of an identifier classified as both internally and externally linked is undefined behavior.
|Axivion Bauhaus Suite|
Missing External Declaration
|LDRA tool suite|
461 S, 575 S, 2 X
401, 839, 1051
|CERT_C-DCL36-a||Identifiers shall not simultaneously have both internal and external linkage in the same translation unit|
|Polyspace Bug Finder|
Checks for inconsistent use of static and extern in object declarations (rule partially covered)
|non-static declaration follows static declaration|
Key here (explains table format and definitions)
|MISRA C:2012||Rule 8.2 (required)||Prior to 2018-01-12: CERT: Unspecified Relationship|
|MISRA C:2012||Rule 8.4 (required)||Prior to 2018-01-12: CERT: Unspecified Relationship|
|MISRA C:2012||Rule 8.8 (required)||Prior to 2018-01-12: CERT: Unspecified Relationship|
|MISRA C:2012||Rule 17.3 (mandatory)||Prior to 2018-01-12: CERT: Unspecified Relationship|
|[Banahan 2003]||Section 8.2, "Declarations, Definitions and Accessibility"|
|[ISO/IEC 9899:2011]||6.2.2, "Linkages of Identifiers"|