Main Content

AUTOSAR C++14 Rule A3-3-1

Objects or functions with external linkage (including members of named namespaces) shall be declared in a header file

Description

Rule Definition

Objects or functions with external linkage (including members of named namespaces) shall be declared in a header file.

Rationale

If you declare a function or object in a header file, it is clear that the function or object is meant to be accessed in multiple translation units. If you intend to access the function or object from a single translation unit, declare it static or in an unnamed namespace.

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:

  • decls.h:

    extern int x;
  • file.cpp:

    #include "decls.h"
    
    int x = 0;
    int y = 0; //Noncompliant
    static int z = 0;

In this example, the variable x is declared in a header file but the variable y is not. The variable z is also not declared in a header file but it is declared with the static specifier and does not have external linkage.

Check Information

Group: Basic Concepts
Category: Required, Automated

Version History

Introduced in R2019a