Home of the Brave
Home of the Brave

Reputation: 153

Why inline functions also need prototypes (MISRA C 2012 Rule 8.4)

Let's say there is a C header file:

//void test(void);

inline void test(void) {}

If we comment out the first line, MISRA C checker will complain

Type: MISRA C-2012 Declarations and Definitions (MISRA C-2012 Rule 8.4, Required) Triage unavailable. header.h:3:

  1. misra_c_2012_rule_8_4_violation: Function definition does not have a visible prototype.

I don't understand why inline functions also need prototypes. Thank you.

Upvotes: 2

Views: 347

Answers (1)

Lundin
Lundin

Reputation: 214780

MISRA C enforces prototypes since it allows C90 where non-prototype function declarations were a huge problem. But also to avoid linker errors and avoid inconsistencies between declaration and definition. Consider for example inline void f() called as f(123) - valid C99 but leading to mysterious linker errors.

8.4 specifically is about enforcing a function declaration before writing a function declaration with external linkage. Most of the time you do not want an inline function to have external linkage. In fact another MISRA C rule 8.10 enforces inline functions to always have internal linkage by declaring them static - and the reason for that is to avoid undefined behavior, as stated by that rule's rationale.

Another feature and/or subtle safety problem is that in case you define a function inside a header, that function will exist in every translation unit that includes the header. So you end up with multiple functions with the same name - typically leading to a linker error. Unless you also give the function internal linkage, in which case the C compiler/linker will utilize name mangling to separate the different functions.

Upvotes: 6

Related Questions