Main Content

MISRA C:2023 Rule 8.4

A compatible declaration shall be visible when an object or function with external linkage is defined

Since R2024a

Description

Rule Definition

A compatible declaration shall be visible when an object or function with external linkage is defined.

Rationale

If a declaration is visible when an object or function is defined, it allows the compiler to check that the declaration and the definition are compatible.

This rule with MISRA C:2012 Rule 8.5 enforces the practice of declaring an object (or function) in a header file and including the header file in source files that define or use the object (or function).

Polyspace Implementation

The rule checker detects situations where:

  • An object or function is defined without a previous declaration.

  • There is a data type mismatch between the object or function declaration and definition. Such a mismatch also causes a compilation error.

The checker now flags tentative definitions (variables declared without an extern specifier and not explicitly defined). To avoid the rule violation, declare the variable static (defined in one file only), or declare the variable extern and follow the declaration with a definition.

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

Header file:

/* file.h */
extern int var2; 
void func2(void);

Source file:

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

int var1 = 0;    /* Non compliant */
int var2 = 0;    /* Compliant */

void func1(void) {   /* Non compliant */
}

void func2(void) {   /* Compliant */
}

In this example, the definitions of var1 and func1 are noncompliant because they are not preceded by declarations.

        void func(int param1, int param2);

void func(int param1, unsigned int param2) { /* Non compliant */
}

In this example, the definition of func has a different parameter type from its declaration. The declaration mismatch might cause a compilation error. Polyspace® flags the mismatch.

Check Information

Group: Declarations and Definitions
Category: Required
AGC Category: Advisory

Version History

Introduced in R2024a