Precautions shall be taken in order to prevent the contents of a header file being included more than once
Precautions shall be taken in order to prevent the contents of a header file being included more than once.
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.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 by using one of the following methods:
<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>If you expect a rule violation but do not see it, refer to the documentation of Polyspace Code Prover™ or Polyspace Code Prover Server™.
| Group: Code Design |
| Category: Required |
| AGC Category: Required |