MISRA C:2012 Rule 8.7
Functions and objects should not be defined with external linkage if they are referenced in only one translation unit
Description
This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).
Rule Definition
Functions and objects should not be defined with external linkage if they are referenced in only one translation unit.
Rationale
Compliance with this rule avoids confusion between your identifier and an identical identifier in another translation unit or library. If you restrict or reduce the visibility of an object by giving it internal linkage or no linkage, you or someone else is less likely to access the object inadvertently.
Polyspace Implementation
The rule checker flags:
Objects that are defined at file scope without the
static
specifier but used only in one file.Functions that are defined without the
static
specifier but called only in one file.
If you intend to use the object or function in one file only, declare it static.
If your code does not contain a main
function and you use
options such as Variables to
initialize (-main-generator-writes-variables)
with value custom
to explicitly specify a set of variables to initialize, the checker does not flag
those variables. The checker assumes that in a real application, the file containing
the main
must initialize the variables in addition to any file
that currently uses them. Therefore, the variables are used in more than one
translation unit.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Declarations and Definitions |
Category: Advisory |
AGC Category: Advisory |
Version History
Introduced in R2014b