Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 6.0.3 and extern "C"
#1
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?)
Reply


Messages In This Thread
Rule 6.0.3 and extern "C" - by kth - 04-05-2026, 03:12 PM
RE: Rule 6.0.3 and extern "C" - by misra cpp - 08-05-2026, 01:35 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)