メインコンテンツ

MISRA C:2025 Rule 8.18

R2026b

There shall be no tentative definitions in a header file

Since R2026b

Description

There shall be no tentative definitions in a header file.1

Rationale

A variable declaration at file scope without the extern keyword and without an initializer is a tentative declaration. Tentative declaration can result in multiple instances of an object, where each source file that includes the header creates a an independent instance. This can be unexpected and can result in incorrect behavior

Polyspace Implementation

Polyspace® reports a violation of this rule when a header file contains:

  • A variable declaration at file scope without the extern keyword and without an initializer. This kind of declaration is referred to as tentative declaration.

  • A function definition (function with a body)

  • A struct definition that simultaneously declares an instance of that struct

  • An array definition at file scope

The checker does not flag:

  • Declarations using the extern keyword

  • Function prototypes without a body

  • struct type definitions without an accompanying instance

  • typedef declarations

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, the header file sensor.h contains several declarations. Polyspace flags declarations that constitute tentative definitions in the header file.

/* sensor.h */
#include <stdint.h>

int32_t reading;             /* Noncompliant - tentative definition */
extern int32_t calibration;  /* Compliant - extern declaration */

void reset(void) {           /* Noncompliant - function definition in header */
    reading = 0;
}

void init(void);             /* Compliant - function prototype */

struct Config {
    int mode;
    int rate;
} defaultCfg;                /* Noncompliant - struct definition with instance */

struct Status {              /* Compliant - struct type without instance */
    int code;
    int flags;
};

typedef struct {             /* Compliant - typedef only */
    int x;
    int y;
} Point;

Point origin;                /* Noncompliant - tentative definition */

int32_t buffer[64];          /* Noncompliant - array definition */

To fix these violations, move variable and function definitions to a source file. In the header file, use extern for variable declarations and provide only function prototypes.

Check Information

Group: Declarations and definitions
Category: Required
AGC Category: Required
PQL Name: std.misra_c_2025.R8_18

Version History

Introduced in R2026b


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C:2025

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.