MISRA Discussion Forums
Rule 6.0.3 and extern "C" - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188)
+---- Forum: 4.6 Basic concepts (https://forum.misra.org.uk/forumdisplay.php?fid=192)
+---- Thread: Rule 6.0.3 and extern "C" (/showthread.php?tid=1767)



Rule 6.0.3 and extern "C" - kth - 04-05-2026

Rule 6.0.3 has the title
Quote:The only declarations in the global namespace should be main, namespace declarations and extern "C" declarations

However, with the current MISRA C++:2023 version the amplification does not contain details about the extern "C" declarations and there are no  examples that use  an extern "C" block.

My question first question: is it OK to use typdefs in extern "C" blocks and also in the global namespace to be able to share these typdefs between C and C++ code?

Example:

Code:
#ifdef __cplusplus
#include <cstdint>
#else
#include <stdint.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

typedef uint16_t AddressType_t;  /* Compliant? (to be able to share typdefs in C and C++ code this is OK) */

void foo();                      /* Compliant? ('foo' needs C linkage and it is OK to have a C function in the global namespace) */

#ifdef __cplusplus
}
#endif


Second question: As the rule explicitly mentions extern "C" declarations - what else is allowed or not allowed? (What about, e.g., struct definitions or function definitions?)


RE: Rule 6.0.3 and extern "C" - misra cpp - 08-05-2026

extern "C" only affects linkage, and MISRA C++:2023 rules do not place any limits on their contents.

The intention of 6.0.3 is to allows extern "C" to be used at global scope so that use can be made to declarations/definitions in headers from other  C-compatible languages. There is an expectation that they will not be used to add objects at global scope that would otherwise be disallowed by 6.0.3.