Main Content

MISRA C++:2023 Rule 19.2.2

The #include directive shall be followed by either a <filename> or "filename" sequence

Since R2024b

Description

Rule Definition

The #include directive shall be followed by either a <filename> or "filename" sequence.

Rationale

This rule applies only after macro replacement.

The code behavior is undefined if an #include directive does not use one of these forms:

  • #include <filename>

  • #include "filename"

ISO/IEC 14882:2003 does not permit using other forms of #include directives. To avoid unexpected behavior, avoid using malformed #include statements.

Polyspace Implementation

Polyspace® reports a violation of this rule if an #include directive is not followed by either <filename> or "filename", where filename is a valid header file or the path to a header file.

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

expand all

This example shows compliant #include directives that use the permissible forms after macro replacement. Polyspace reports violations for malformed #include directives that do not use the permissible forms.

#include "incguard.h"     //Compliant
#include <incguard.h>     //Compliant

#define MH "myheader.h"
#define STR <string>    //Compliant
#include MH             //Compliant
#include STR            //Compliant

#include myfile.h       //Noncompliant
#include iostream       //Noncompliant
#include math.h         //Noncompliant
#include "math.h>       //Noncompliant
#include >myHeader.h<   //Noncompliant
#include <math.h        //Noncompliant
#include math.h>        //Noncompliant

Check Information

Group: Preprocessing Directives
Category: Required

Version History

Introduced in R2024b