MISRA C:2012 Dir 4.10
Precautions shall be taken in order to prevent the contents of a header file being included more than once
Description
Directive Definition
Precautions shall be taken in order to prevent the contents of a header file being included more than once.
Rationale
When a translation unit contains a complex hierarchy of nested header files, it is possible for a particular header file to be included more than once, leading to confusion. If this multiple inclusion produces multiple or conflicting definitions, then your program can have undefined or erroneous behavior.
For instance, suppose that a header file contains:
#ifdef _WIN64 int env_var; #elseif long int env_var; #endif
_WIN64
and another that undefines it, you can have
conflicting definitions of env_var
.Polyspace Implementation
If you include a header file whose contents are not guarded from multiple inclusion, the analysis raises a violation of this directive. The violation is shown at the beginning of the header file.
You can guard the contents of a header file from multiple inclusion using several methods. For
instance, use the preprocessor directives ifdef
or
ifndef
as include guards :
<start-of-file> #ifndef <control macro> #define <control macro> /* Contents of file */ #endif <end-of-file>
<start-of-file> #ifdef <control macro> #error ... #else #define <control macro> /* Contents of file */ #endif <end-of-file>
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: Code Design |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2014b