Main Content

MISRA C++:2008 Rule 3-2-4

An identifier with external linkage shall have exactly one 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 definition.

Rationale

If an identifier has multiple definitions or no definitions, it can lead to undefined behavior.

Polyspace Implementation

Polyspace

reports a variable if any of these conditions are true:

  • You specify multiple definitions or tentative definitions of a symbol.

  • There are undefined global variables in your code.

The checker is not raised on unused code such as

  • Noninstantiated templates

  • Uncalled static or extern functions

  • Uncalled and undefined local functions

  • Unused types and variables

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

This example uses two files:

  • file1.cpp:

    typedef signed   int          int32_t;
    
    namespace NS {
    	extern int32_t a; //Noncompliant
    
    	void foo(){
    		a = 0;
    		
    	}
    }; 
  • file2.cpp:

    typedef signed   int          int32_t;
    typedef signed   long long         int64_t;
    
    namespace NS {
    	extern int64_t a; //Noncompliant
    	void bar(){
    		++a;
    		
    	}
    };

The same identifier a is defined in both files.

Check Information

Group: Basic Concepts
Category: Required

Version History

Introduced in R2013b