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 unit1 .
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 you use options such as Inputs
(-variables-written-in-loop)
and Parameters
(-variables-written-before-loop)
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
these 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
See Also
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.