Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens with an extern inline function?

What happens if I define the 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 inline, but what if it does not ignore it in this case?

like image 545
musicmatze Avatar asked Jul 06 '13 15:07

musicmatze


2 Answers

Having added the inline to the function definition in the .c file is just superfluous.

  • Your compilation unit of the .c file sees an extern declaration (without inline) and an inline 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

like image 172
Jens Gustedt Avatar answered Nov 14 '22 16:11

Jens Gustedt


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.

like image 6
Yu Hao Avatar answered Nov 14 '22 16:11

Yu Hao