Main Content

MISRA C++:2008 Rule 3-1-1

It shall be possible to include any header file in multiple translation units without violating the One Definition Rule

Description

Rule Definition

It shall be possible to include any header file in multiple translation units without violating the One Definition Rule.

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 instance, a source file includes the header file include.h and another header file, which also includes include.h.

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

expand all

In this example, Polyspace reports violations of this rule when variables and function definitions are placed in the header file header.hpp.

// header.hpp
#include <cstdint>
void F1();        // Compliant
extern void F2(); // Compliant
void F3()         // Noncompliant
{
}
static inline void F4()
{
} // Compliant
template <typename T>
void F5(T) // Compliant
{
}
std::int32_t a;                       // Noncompliant
extern std::int32_t b;                // Compliant
constexpr static std::int32_t c = 10; // Compliant
namespace ns
{
    constexpr static std::int32_t d = 100; // Compliant
    const static std::int32_t e = 50;      // Compliant
    static std::int32_t f;                 // Noncompliant
    static void F6() noexcept;             // Noncompliant
}

//source.cpp
#include "header.hpp"
void foo()
{
	//...
}

Check Information

Group: Basic Concepts
Category: Required

Version History

Introduced in R2013b