2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-04-28 16:49:39 +02:00
|
|
|
Having a single declaration of a type, object or function allows the compiler to detect incompatible types for the same entity.
|
|
|
|
|
|
|
|
|
|
|
|
Normally, this will mean declaring an external identifier in a header file that will be included in any file where the identifier is defined or used.
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,cpp]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
// header.hpp
|
|
|
|
extern int16_t a;
|
|
|
|
|
|
|
|
// file1.cpp
|
|
|
|
#include "header.hpp"
|
|
|
|
|
|
|
|
extern int16_t b;
|
|
|
|
|
|
|
|
// file2.cpp
|
|
|
|
#include "header.hpp"
|
|
|
|
extern int32_t b; // Noncompliant, compiler may not detect the error
|
|
|
|
int32_t a; // Compliant, compiler will detect the error
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,cpp]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
// header.hpp
|
|
|
|
extern int16_t a; // Compliant, declared once in a header
|
|
|
|
extern int32_t b; // Compliant, declared once in a header
|
|
|
|
|
|
|
|
// file1.cpp
|
|
|
|
#include "header.hpp"
|
|
|
|
|
|
|
|
// file2.cpp
|
|
|
|
#include "header.hpp"
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Resources
|
2021-04-28 16:49:39 +02:00
|
|
|
|
|
|
|
* MISRA {cpp}:2008, 3-2-3
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Move the declaration of "xxx" to a header file.
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== relates to: S826
|
|
|
|
|
|
|
|
=== relates to: S828
|
|
|
|
|
|
|
|
=== relates to: S829
|
|
|
|
|
|
|
|
=== relates to: S831
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|