Inline functions in C99 mode
The rules for C99 inline functions with external linkage differ
to those of C++. C99 distinguishes between inline definitions and
external definitions. Within a given translation unit where the
inline function is defined, if the inline function is always declared
with inline and never with extern, it
is an inline definition. Otherwise, it is an external definition.
These inline definitions are not used to generate out-of-line copies,
even when --no_inline is used.
Each use of an inline function might be inlined using a definition
from the same translation unit (that might be an inline definition
or an external definition), or it might become a call to an external
definition. If an inline function is used, it must have exactly
one external definition in some translation unit. This is the same
rule that applies to using any external function. In practise, if
all uses of an inline function are inlined, no error occurs if the
external definition is missing. If you use --no_inline,
only external definitions are used.
Typically, you put inline functions with external linkage
into header files as inline definitions, using inline,
and not using extern. There is also an external definition
in one source file. For example:
Example 11. Function inlining in C99
/* example_header.h */
inline int my_function (int i)
{
return i + 42; // inline definition
}
/* file1.c */
#include "example_header.h"
... // uses of my_function()
/* file2.c */
#include "example_header.h"
... // uses of my_function()
/* myfile.c */
#include "example_header.h"
extern inline int my_function(int); // causes external definition.
This is the same strategy that is typically used for C++,
but in C++ there is no special external definition, and no requirement
for it.
The definitions of inline functions can be different in different
translation units. However, in typical use, like in example Example 11, they are identical.
When compiling with --multifile or --ltcg,
calls in one translation unit might be inlined using the external
definition in another translation unit.
C99 places some restrictions on inline definitions. They cannot
define modifiable local static objects. They cannot reference identifiers
with static linkage.
In C99 mode, as with all other modes, the effects of __inline and inline are
identical.
Inline functions with static linkage have the same behavior
in C99 as in C++.
See also