What does extern inline do?
c, c++, extern, inline-functions
Solution
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 (compilation might occasionally succeed depending on compiler options).
- `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 (full) 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). Some compilers will omit exporting the symbol if it isn't used locally as an optimization.
Problem
I understand that `inline` by itself is a suggestion to the compiler, and at its discretion it may or may not inline the function, and it will also produce linkable object code. I think that `static inline` does the same (may or may not inline) but will not produce linkable object code when inlined (since no other module could link to it). Where does `extern inline` fit into the picture? Assume I want to replace a preprocessor macro by an inline function and require that this function gets inlined (e.g., because it uses the `__FILE__` and `__LINE__` macros which should resolve for the caller but not this called function). That is, I want to see a compiler or linker error in case the function does not get inlined. Does `extern inline` do this? (I assume that, if it does not, there is no way to achieve this behavior other than sticking with a macro.) Are there differences between C++ and C? Are there differences between different compiler vendors and versions?