주요 콘텐츠

MISRA C:2025 Rule 8.19

R2026b

There should be no external declarations in a source file

Since R2026b

Description

There should be no external declarations in a source file. 1

Rationale

An extern declaration in a source file creates a dependency that is not expressed through the header include mechanism. If the type or signature of the declared symbol changes in the defining translation unit, the compiler cannot detect the mismatch in other translation units that redeclare the symbol locally. Declaring all external interfaces in header files centralizes the interface and makes it easier to address changes to the external functions and variables.

Polyspace Implementation

The coding rule checker reports a violation of this rule when a nonheader source file contains an extern declaration of an object or function. The declaration can appear at file scope or at block scope inside a function body.

No violation is reported when the extern declaration appears in a header file.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, the source file redeclares symbols with extern instead of including the header that provides those declarations.


/* control.c */
#include <stdint.h>

extern int32_t sensor_reading;            // Noncompliant
extern int32_t calibrate_sensor(int32_t); // Noncompliant

int32_t apply_offset(int32_t offset) {
    return calibrate_sensor(sensor_reading + offset);
}

Polyspace® flags both extern declarations in control.c because they appear in a source file rather than in a header file.

Correction — Include Header File

Replace the local extern declarations with a header file include.


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

extern int32_t sensor_reading;
extern int32_t calibrate_sensor(int32_t);

/* control.c */
#include "sensor.h"

int32_t apply_offset(int32_t offset) {    // Compliant
    return calibrate_sensor(sensor_reading + offset);
}

In this example, extern declarations appear at block scope inside a function body, bypassing the header interface.


/* diagnostics.c */
#include <stdbool.h>

void run_diagnostics(void) {
    extern bool error_flag;          // Noncompliant
    extern void log_error(bool);     // Noncompliant

    log_error(error_flag);
}

Polyspace flags both block-scope extern declarations because they bring symbols into scope without a modular header interface.

Correction — Move Declarations to Header File

Declare the external symbols in a header file and include that header.


/* error_handler.h */
#include <stdbool.h>

extern bool error_flag;
extern void log_error(bool);

/* diagnostics.c */
#include "error_handler.h"

void run_diagnostics(void) {           // Compliant
    log_error(error_flag);
}

Check Information

Group: Declarations and definitions
Category: Advisory
AGC Category: Advisory
PQL Name: std.misra_c_2025.R8_19

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.