Main Content

MISRA C:2012 Rule 8.6

An identifier with external linkage shall have exactly one external definition

Description

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

Rule Definition

An identifier with external linkage shall have exactly one external definition.

Rationale

If you use an identifier for which multiple definitions exist in different files or no definition exists, the behavior is undefined.

Multiple definitions in different files are not permitted by this rule even if the definitions are the same.

Polyspace Implementation

The checker flags multiple definitions only if the definitions occur in different files.

The checker does not consider tentative definitions as definitions. For instance, the following code does not violate the rule:

int val;
int val=1;

The checker does not show a violation if a function is not defined at all but declared with external linkage and called in the source code.

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

First source file:

/* file1.c */
extern int var = 1; /* Non compliant */

Second source file:

/* file2.c */
int var = 0;   
In this example, the global variable var is multiply defined. Unless explicitly specified with the static qualifier, the variables have external linkage.

Header file:

/* file.h */
int func(int param);

First source file:

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

int func(int param) { /* Non compliant */
    return param+1;
}

Second source file:

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

int func(int param) {  
    return param-1;
}

In this example, the function func is multiply defined. Unless explicitly specified with the static qualifier, the functions have external linkage.

Check Information

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

Version History

Introduced in R2014b