AutosarC++17_10-A3.3.1

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

Required inputs: IR

Rationale

Placing the declarations of objects and functions with external linkage in a header file means that they are intended to be accessible from other translation units.

If external linkage is not needed, then the object or function is supposed to be either declared in an unnamed namespace or declared static in the implementation file.

This reduces the visibility of objects and functions, which allows to reach a higher encapsulation and isolation.

Note that members of named namespace are by default external linkage objects.

Exceptions

This rule does not apply to main, or to members of unnamed namespaces.

Example
//% $Id: A3-3-1.hpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
extern std::int32_t a1;
extern void F4();
namespace n {
void F2();
std::int32_t a5; // Compliant, external linkage
} // namespace n
//% $Id: A3-3-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include "A3-3-1.hpp"
std::int32_t a1 = 0;        // Compliant, external linkage
std::int32_t a2 = 0;        // Non-compliant, static keyword not used
static std::int32_t a3 = 0; // Compliant, internal linkage
namespace {
std::int32_t a4 = 0; // Compliant by exception
void F1()            // Compliant by exception
{}
} // namespace
namespace n {
void F2() // Compliant, external linkage
{}
std::int32_t a6 = 0; // Non-compliant, external linkage
} // namespace n
extern std::int32_t a7; // Non-compliant, extern object declared in .cpp file
static void F3()        // Compliant, static keyword used
{}
void F4() // Compliant, external linkage
{
  a1 = 1;
  a2 = 1;
  a3 = 1;
  a4 = 1;
  n::a5 = 1;
  n::a6 = 1;
  a7 = 1;
}
void F5() // Non-compliant, static keyword not used
{
  a1 = 2;
  a2 = 2;
  a3 = 2;
  a4 = 2;
  n::a5 = 2;
  n::a6 = 2;
  a7 = 2;
}
int main(int, char **) // Compliant by exception
{
  F1();
  n::F2();
  F3();
  F4();
  F5();
}
See Also
  • MISRA C++ 2008 [6]: Rule 3-3-1 Objects or functions with external linkage shall be declared in a header file.

Possible Messages

Key

Text

Severity

Disabled

missing_function_declaration_in_header

Object or function with external linkage shall be declared in a header file

None

False

missing_variable_declaration_in_header

Object or function with external linkage shall be declared in a header file

None

False

Options

restrict_to_namespace_scope

restrict_to_namespace_scope : bool = False

Exclude object or function not in namespace or global scope.