musicmatze musicmatze - 1 year ago 88
C Question

What happens with an extern inline function?

What happens if I define my function in my .h file as

extern int returnaint(void);

, define it in the related .c file as

inline int returnaint(void) {
return 1;

and include the header in another .c file and use the function? When I compile the things seperatly, creating a object file for each .c file and then link them, is the inlined function included, or what happens?

I know the compiler can ignore
, but what if it does not ignore it in this case?

Answer Source

It won't compile. From C11 (ISO/IEC 9899:2011) ยง6.7.4 Function specifiers (emphasis added):

Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit. If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition.140)

140)Since an inline definition is distinct from the corresponding external definition and from any other corresponding inline definitions in other translation units, all corresponding objects with static storage duration are also distinct in each of the definitions.

The other .c file gets only the declaration of the inline function from the header, but not the definition, so it's against the rule in bold font.


As @Jens Gustedt points out, my previous explanation is wrong, because in the OP's question, the function is declared as non-inline in the header file:

extern int returnaint(void);

So the other .c file will treat it like a normal function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download