MISRA C:2012 Rule 8.5

An external object or function shall be declared once in one and only one file

Description

This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Default Analysis.

Rule Definition

An external object or function shall be declared once in one and only one file.

Rationale

If you declare an identifier in a header file, you can include the header file in any translation unit where the identifier is defined or used. In this way, you ensure consistency between:

  • The declaration and the definition.

  • The declarations in different translation units.

The rule enforces the practice of declaring external objects or functions in header files.

Polyspace Implementation

The rule checker checks only explicit extern declarations (tentative definitions are ignored). The checker flags variables or functions declared extern in a non-header file.

Polyspace Bug Finder™ and Polyspace Code Prover™ check this coding rule differently. The analyses can produce different results.

Additional Message in Report

  • Object object_name has external declarations in multiple files.

  • Function function_name has external declarations in multiple files.

Troubleshooting

If you expect a rule violation but do not see it, refer to the documentation of Polyspace Code Prover or Polyspace Code Prover Server™.

Examples

expand all

Header file:

/* file.h */
extern int var;
extern void func1(void);   /* Compliant */

Source file:

/* file.c */
#include "file.h"

extern void func2(void);   /* Non compliant */

/* Definitions */
int var = 0;
void func1(void) {}
In this example, the declaration of external function func2 is noncompliant because it occurs in a non-header file. The other external object and function declarations occur in a header file and comply with this rule.

Check Information

Group: Declarations and Definitions
Category: Required
AGC Category: Advisory
Introduced in R2014b