What does extern inline do?

in K&R C or C89, inline was not part of the language. Many compilers implemented it as an extension, but there were no defined semantics regarding how it worked. GCC was among the first to implement inlining, and introduced the inline, static inline, and extern inline constructs; most pre-C99 compiler generally follow its lead.

GNU89:

  • inline: the function may be inlined (it’s just a hint though). An out-of-line version is always emitted and externally visible. Hence you can only have such an inline defined in one compilation unit, and every other one needs to see it as an out-of-line function (or you’ll get duplicate symbols at link time).
  • extern inline will not generate an out-of-line version, but might call one (which you therefore must define in some other compilation unit. The one-definition rule applies, though; the out-of-line version must have the same code as the inline offered here, in case the compiler calls that instead.
  • static inline will not generate a externally visible out-of-line version, though it might generate a file static one. The one-definition rule does not apply, since there is never an emitted external symbol nor a call to one.

C99 (or GNU99):

  • inline: like GNU89 “extern inline”; no externally visible function is emitted, but one might be called and so must exist
  • extern inline: like GNU89 “inline”: externally visible code is emitted, so at most one translation unit can use this.
  • static inline: like GNU89 “static inline”. This is the only portable one between gnu89 and c99

C++:

A function that is inline anywhere must be inline everywhere, with the same definition. The compiler/linker will sort out multiple instances of the symbol. There is no definition of static inline or extern inline, though many compilers have them (typically following the gnu89 model).

Leave a Comment