Your code is incorrect because you cannot declare a function with extern (by default) and then provide a static definition. The fact that it compiles at all does not indicate anything useful.
From n1548 §6.2.2:
If the same identifier with internal and external relationships appears within the translation unit, the behavior is undefined.
So you will get something like this in file1.c :
// Has external linkage (which is the default!) extern inline void fun1(void); // This would also have external linkage. inline void fun1(void); // This has static linkage. static inline void fun1(void) { ... }
(Note: "external snapping" is the default, but extern inline actually means something special, it is different from inline .)
Bam! Undefined. The compiler may not even give you an error message, although some compilers seem to provide error messages for this.
error: static declaration of 'func' follows non-static declaration
This error actually has nothing to do with the inline function. This is a bug with or without inline .
What about these questions?
static inline functions are visible only for the translation unit where it is defined.
This applies to all static functions. They have an “internal connection”, so you can have static void func(void); in one file and a completely different static int func(char *p); in another file. inline doesn't matter here.
External built-in functions may be available in more than a few translation units.
Yes, so you should not put them in header files. If you put them in the header files, you will get several different definitions of the same function, which can be accessed from different translation units. This is mistake. Instead, put the extern inline in the source files, but this should only be an declaration, not a definition.
Better define inline functions in header files
There is no real point to define an inline function elsewhere. If your function is used in only one file, just mark it static and the compiler will decide how to call the function.
There is no difference between the definitions of static and static inline functions.
Yes, no difference.
Well, technically, no, there is a difference, because the compiler is allowed to handle static inline functions differently than just static functions. However, modern compilers tend to solve built-in functions using their own set of rules, and the inline function does not greatly affect this process.
Well, practically there is one more difference. The definition of the static inline function will not generate a warning in GCC if it is not used, but the static function will. This means that you can put the static inline function in the header file. This is an alternative to putting inline in a header file, which requires that you have an extern inline for this function somewhere in your program. However, if the compiler decides that it is more likely not to embed your static inline function because it considers the attachment to be worse or because the attachment is not possible, then it will have to make a separate copy of this function in each file that it used in.
So how do you do it right?
Never declare a static function if it has a previous non-static declaration. This is a mistake even if it compiles.
Do not declare the inline extern function in the header file. This creates an “external definition” for the function, and you can only have one of them in your program.
Do not declare inline functions in header files without defining them. It makes no sense.
Here is how you would like to do it:
In mylib.h :
// Provide "inline definition" of the function. inline int times_two(int x) { return x * 2; }
In mylib.c :
#include "mylib.h"
This is the standard way to do something with the C99. The compiler should have the right to use an internal definition or an external definition, depending on what it thinks is better. If you do not have an external definition or you have more than one, you may receive a binding error, as with regular functions.
C ++ has its own completely different rules for inline functions.