Main Content

MISRA C++:2023 Rule 6.5.1

A function or object with external linkage should be introduced in a header file

Since R2024b

Description

Rule Definition

A function or object with external linkage should be introduced in a header file.

Rationale

If you declare a function or object in a header file, it is clear that the function or object is meant to be accessed in multiple translation units. If you intend to access the function or object from a single translation unit, declare it static or in an unnamed namespace.

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 uses two files:

  • decls.h:

    extern int x;
  • file.cpp:

    #include "decls.h"
    
    int x = 0;
    int y = 0; //Noncompliant
    static int z = 0;

In this example, the variable x is declared in a header file but the variable y is not. The variable z is also not declared in a header file but it is declared with the static specifier and does not have external linkage.

Check Information

Group: Basic Concepts
Category: Advisory

Version History

Introduced in R2024b