When declaring an extern
function to be inline, you must define it in
every translation unit that it is used in. You must ensure that you use the same definition
in each translation unit.
The requirement of defining the function in every translation unit applies even though it
has external linkage.
If an inline function is used by more than one translation unit, its definition is
typically placed in a header file.
ARM does not recommend placing definitions of non-inline functions in header files, because
this can result in the creation of a separate function in each translation unit. If the
non-inline function is an extern
function, this leads to duplicate
symbols at link time. If the non-inline function is static
, this
can lead to unwanted code duplication.
Member functions defined within a C++ structure, class, or union declaration, are
implicitly inline. They are treated as if they are declared with the
inline
or __inline
keyword.
Inline functions have extern
linkage unless they are explicitly
declared static
. If an inline function is declared to be static,
any out-of-line copies of the function must be unique to their translation unit, so
declaring an inline function to be static could lead to unwanted code duplication.
The compiler generates a regular call to an out-of-line copy of a function when it cannot
inline the function, and when it decides not to inline it.
The requirement of defining a function in every translation unit it is used in means that
the compiler is not required to emit out-of-line copies of all
extern
inline functions. When the compiler does emit out-of-line
copies of an extern
inline function, it uses Common Groups, so
that the linker eliminates duplicates, keeping at most one copy in the same out-of-line
function from different object files.