Note: when I talk about .c
files and .h
files in this answer, I assume you have laid out your code correctly, i.e. .c
files only include .h
files. The distinction is that a .h
file may be included in multiple translation units.
static inline void f(void) {}
has no practical difference with static void f(void) {}
.
In ISO C, this is correct. They are identical in behaviour (assuming you don't re-declare them differently in the same TU of course!) the only practical effect may be to cause the compiler to optimize differently.
inline void f(void) {}
in C doesn't work as the C++ way. How does it work in C? What actually does extern inline void f(void);
do?
This is explained by this answer and also this thread.
In ISO C and C++, you can freely use inline void f(void) {}
in header files -- although for different reasons!
In ISO C, it does not provide an external definition at all. In ISO C++ it does provide an external definition; however C++ has an additional rule (which C doesn't), that if there are multiple external definitions of an inline
function, then the compiler sorts it out and picks one of them.
extern inline void f(void);
in a .c
file in ISO C is meant to be paired with the use of inline void f(void) {}
in header files. It causes the external definition of the function to be emitted in that translation unit. If you don't do this then there is no external definition, and so you may get a link error (it is unspecified whether any particular call of f
links to the external definition or not).
In other words, in ISO C you can manually select where the external definition goes; or suppress external definition entirely by using static inline
everywhere; but in ISO C++ the compiler chooses if and where an external definition would go.
In GNU C, things are different (more on this below).
To complicate things further, GNU C++ allows you to write static inline
an extern inline
in C++ code... I wouldn't like to guess on what that does exactly
I never really found a use of the inline keyword in my C programs, and when I see this keyword in other people's code, it's almost always static inline
Many coders don't know what they're doing and just put together something that appears to work. Another factor here is that the code you're looking at might have been written for GNU C, not ISO C.
In GNU C, plain inline
behaves differently to ISO C. It actually emits an externally visible definition, so having a .h
file with a plain inline
function included from two translation units causes undefined behaviour.
So if the coder wants to supply the inline
optimization hint in GNU C, then static inline
is required. Since static inline
works in both ISO C and GNU C, it's natural that people ended up settling for that and seeing that it appeared to work without giving errors.
, in which I see no difference with just static.
The difference is just in the intent to provide a speed-over-size optimization hint to the compiler. With modern compilers this is superfluous.
inline
is of use in C, and the link leads to a C++ question which just adds to confusion. – Memorize.h
and theextern inline
declaration in exactly one.c
to instantiate a non-inline definition and make a working C program with a non-staticinline
function defined in a header and thus able to actually inline. In C, should inline functions in headers be externed in the .c file? is also about that, with a concise answer. – Reposition