MISRA C++:2023 Rule 6.2.4
A header file shall not contain definitions of functions or objects that are non-inline and have external linkage
Since R2024b
Description
Rule Definition
A header file shall not contain definitions of functions or objects that are non-inline and have external linkage.
Rationale
If a header file with variable or function definitions appears in multiple inclusion
paths, the header file violates the One Definition Rule possibly leading to unpredictable
behavior. For example, consider a header file myHeader.h
that has a
function definition. If this header is included in your analysis multiple time, the function
violates the One Definition Rule. This can happen when you include
myHeader.h
and another header file which in-turn also includes
myHeader.h
.
Avoid definition of these non-inline entities in a header file:
Namespace-scope variables
namespace-scope function
Static and nonstatic member functions
Non-
const
static data member
Polyspace Implementation
The rule checker flags variable and function definitions in header files.
Polyspace® reports violation of this rule in header files. In a nonheader source file, violation of this rule is not reported.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Basic Concepts |
Category: Required |
Version History
Introduced in R2024b