Main Content

MISRA C:2012 Rule 8.8

The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage

Description

Rule Definition

The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage.

Rationale

If you do not use the static specifier consistently in all declarations of objects with internal linkage, you might declare the same object with external and internal linkage.

In this situation, the linkage follows the earlier specification that is visible (C99 Standard, Section 6.2.2). For instance, if the earlier specification indicates internal linkage, the object has internal linkage even though the latter specification indicates external linkage. If you notice the latter specification alone, you might expect otherwise.

Polyspace Implementation

The rule checker detects situations where:

  • The same object is declared multiple times with different storage specifiers.

  • The same function is declared and defined with different storage specifiers.

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

static int foo = 0;
extern int foo;         /* Non-compliant */

extern int hhh;
static int hhh;         /* Non-compliant */

In this example, the first line defines foo with internal linkage. The first line is compliant because the example uses the static keyword. The second line does not use static in the declaration, so the declaration is noncompliant. By comparison, the third line declares hhh with an extern keyword creating external linkage. The fourth line declares hhh with internal linkage, but this declaration conflicts with the first declaration of hhh.

Correction — Consistent static and extern Use

One possible correction is to use static and extern consistently:

static int foo = 0;
static int foo;

extern int hhh;
extern int hhh;
static int fee(void);  /* Compliant - declaration: internal linkage */
int fee(void){         /* Non-compliant */
  return 1;
}

static int ggg(int);  /* Compliant - declaration: internal linkage */
extern int ggg(int x){  /* Non-compliant */
  return 1 + x;
}

This example shows two internal linkage violations. Because fee and ggg have internal linkage, you must use a static class specifier in the definition to be compliant with MISRA™.

Check Information

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

Version History

Introduced in R2014b