42

What's the difference between a static inline, extern inline and a normal inline function?

I've seen some vague explanations about this. As far as I've understood, static inline is not just an inline function that is meant to only be referred to within a certain file as the static keyword usually means. The same goes for extern inline too I guess, it's not the same explanation as with extern variables. Any answers would be greatly appreciated!

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
Forever a noob
  • 689
  • 1
  • 9
  • 16
  • possible duplicate: http://stackoverflow.com/questions/7762731/whats-the-difference-between-static-and-static-inline-function –  Jul 28 '14 at 17:13
  • 1
    all are inline functions. static inline is where is a [static](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program) function, [extern](http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/) inline is a externally declared function, normal inline is simply normal. – MAKZ Jul 28 '14 at 17:14
  • possible duplicate: http://stackoverflow.com/questions/216510/extern-inline – Hsi-Hung Shih Jul 28 '14 at 17:15
  • 1
    also read http://stackoverflow.com/a/7767858/2204022 – MAKZ Jul 28 '14 at 17:15
  • Possible duplicate of [extern inline](https://stackoverflow.com/questions/216510/extern-inline) – o11c Jul 08 '18 at 06:43

1 Answers1

38

A function definition with static inline defines an inline function with internal linkage. Such function works "as expected" from the "usual" properties of these qualifiers: static gives it internal linkage and inline makes it inline. So, this function is "local" to a translation unit and inline in it.

A function definition with just inline defines an inline function with external linkage. However, such definition is referred to as inline definition and it does not work as external definition for that function. That means that even though this function has external linkage, it will be seen as undefined from other translation units, unless you provide a separate external definition for it somewhere.

A function definition with extern inline defines an inline function with external linkage and at the same time this definition serves as external definition for this function. It is possible to call such function from other translation units.

The last two paragraphs mean that you have a choice of providing a single extern inline definition for an inline function with external linkage, or providing two separate definitions for it: one inline and other extern. In the latter case, when you call the function the compiler is allowed to chose either of the two definitions.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 2
    Clarifying your last paragraph: to provide the two separate definitions it is not necessary (and in fact, not advisable) to provide two function bodies. Instead `extern inline void f();`, which looks like (and is) a declaration, has the effect of transforming an earlier `inline void f() { ..... }` to be an external definition, instead of an inline definition, for that unit. (Ref. C11 6.7.4/7). – M.M Jun 29 '15 at 07:04
  • 9
    Reading this answer, I'm having trouble understanding the exact meaning of "external definition" vs "internal definition". Where can I find more on that? – learnlearnlearn Apr 07 '19 at 14:19
  • "or providing two separate definitions" `inline int il_fun0(void) { return 0; } extern int il_fun0(void) { return 0; }` results in a C11 compile error of "error: redefinition of 'il_fun0'" (with or without the `extern`). ` – chux - Reinstate Monica Dec 09 '20 at 16:41
  • @learnlearnlearn Inline definition is used for function inlining. External definition is used for function call as usual. The `inline` keyword is just hint to compiler, so you can't assure whether your function call would be compiled to the inlining or normal call. As a result, we should prepare two definitions for the one same inline function. Each translation unit calling the inline function must have its own inline definition for it to handle inlining. Furthermore, Only one translation unit must have external definition to handle normal call. – rosshjb Jan 26 '21 at 03:24
  • @learnlearnlearn For example, when you turn off optimization option of your compiler, inline function call would be compiled to normal function call. In this case, all normal calls refer the only one same external definition. Whereas when you turn on optimization option, inline function call would be compiled to inlining. In this case, the inlining is achieved by each translation unit's its own inline definition. So we put `inline void func(void) {}` in header file for inlining and put `extern inline void func(void);` in source file(separate translation unit) for normal call. – rosshjb Jan 26 '21 at 03:25