What happens with an extern inline function?
Solution 1:
Having added the inline
to the function definition in the .c
file is just superfluous.
Your compilation unit of the
.c
file sees anextern
declaration (withoutinline
) and aninline
definition. Thus it emits the symbol for the function in the object file.All other compilation units only see an
extern
declaration, and so they can use the function without problems, if you link your final executable with the other.o
file.
In fact, you just have it the wrong way around. This feature is meant to be used that you have the inline
defintion in the .h
file, visible to everybody. This definition of the function only acts as a declaration of the symbol, just as extern
would, but doesn't define it.
An extern
declaration in just one .c
file (compilation unit) then ensures such that the symbol is defined, there.
The terminology is a bit confusing, the inline
definition acting as declaration of the symbol, and the extern
declaration acting as definition of it
Solution 2:
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.
EDIT:
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.