It could generate smaller code. The reason is that inline
(as opposed to static inline
) will give the function external linkage so that all calls to the function from different translation units refer to the same logical function. With static inline
, each translation unit will get a unique instance of the function instead, which could increase code size if the compiler chooses not to inline. (It's also cleaner codewise to not have multiple identical functions.)
The reason you need extern
somewhere is because it makes the compiler generate an external definition of the function that can be called from other translation units. Without extern
, no such instance is generated. The no-extern
case differs from internal linkage in that the inline
definition only provides an "alternative" to the external definition of the function. An external definition must still exist (i.e., some translation function must use extern
to generate an external definition), and the compiler is free to use it instead of it wants to.
Here's some relevant standardese (for C11: ISO/IEC 9899:2011 §6.7.4 Function specifiers, ¶7):
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.
By the way, inline
IMO often isn't worthwhile (as a hint -- the compiler is still free to not inline) compared to simply letting the compiler choose when to inline purely on its own. For modern compilers that support link-time optimization, the compiler can even inline functions across translation units if you pass the right flags (e.g., -flto
in GCC).