I've read quite a few posts about the use of static inline
and inline
while defining functions in header files for access across multiple translation units. It seems like inline
is the right way to go due to having external linkage.
My question is about the resulting code size as a result of using inline
specifier when defining functions in .h files:
Is the code expansion generated by inline
still lesser than what would be caused by static inline
?
Why is there a need for an extern inline
declaration in the corresponding .c file ?
static inline generally works as static, but the inline keyword suggest compiler trying to inline this function. In C++11 it is recommended to use function in anonymous namespace over static functions. What if we add the following code into header.hpp: You may notice that this is exactly the same as static, except for its mangled name.
IF the inline function is a “helper” function that’s only used inside one C module, THEN put it in that .c file only and don’t mention it in the header file. This is consistent with Rule 4.2.c, which says that “The header file shall identify only the [functions] … about which it is strictly necessary for other modules to know.”
Any C++ function may be declared inline. But if the inline function is a public member function (a.k.a., public method) of the class it is necessary to place the code for the inline function inside the header file. This is so that all of the other modules that use the class can see the code they need to have placed inline by the compiler.
This overhead occurs for small functions because execution time of small function is less than the switching time. C++ provides an inline functions to reduce the function call overhead. Inline function is a function that is expanded in line when it is called.
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 theinline
function specifier withoutextern
, 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With