If a file-scope object or a function does not need to be visible outside of the file, it should be hidden by being declared as
static. This practice creates more modular code and limits pollution of the global name space.
Subclause 6.2.2 of the C Standard [ISO/IEC 9899:2011] states:
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.
If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.
Noncompliant Code Example
This noncompliant code example includes a
helper() function that is implicitly declared to have external linkage:
This compliant solution declares
helper() to have internal linkage, thereby preventing external functions from using it:
Allowing too many objects to have external linkage can use up descriptive identifiers, leading to more complicated identifiers, violations of abstraction models, and possible name conflicts with libraries. If the compilation unit implements a data abstraction, it may also expose invocations of private functions from outside the abstraction.
|Axivion Bauhaus Suite|
|LANG.STRUCT.SCOPE.FILE||Scope could be file static|
|LDRA tool suite|
Objects or functions with external linkage shall be declared in a header file
|Polyspace Bug Finder|
Functions and objects should not be defined with external linkage if they are referenced in only one translation unit
The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage
|SEI CERT C++ Coding Standard||VOID DCL15-CPP. Declare file-scope objects or functions that do not need external linkage in an unnamed namespace|
Rule 8.7 (advisory)
|ISO/IEC 9899:2011||Subclause 6.2.2, "Linkages of Identifiers"|