>
>
>
V2622. MISRA. External object or functi…


V2622. MISRA. External object or function should be declared once in one and only one file.

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guide.

This rule only applies to programs written in C. Objects or functions with external linkage should be declared once.

Look at the following example:

/* lib1.h */
extern int32_t var; // Declaration

/* lib2.h */
extern int32_t var; // Declaration

/* some.cpp */
#include "lib1.h"
#include "lib2.h"

In this example, the 'var' variable is declared twice: in 'lib1.h' and 'lib2.h'.

We have several ways to fix this:

  • If the 'some.cpp' file contains an extra header file, we can exclude it.
  • If one of the header files contains an extra declaration of the 'var' variable, we can exclude it.
  • We can declare the 'var' variable in a more generalized header file and include it wherever it is used:
/* lib.h */
extern int32_t var; // Declaration

/* lib1.h */
#include "lib.h"

/* lib2.h */
#include "lib.h"

/* some.cpp */
#include "lib1.h"
#include "lib2.h"

This diagnostic is classified as:

  • MISRA-C-8.5