8

I've read quite a few posts about the use of static inline and inline while defining functions in header files for access across multiple translation units. It seems like inline is the right way to go due to having external linkage.

My question is about the resulting code size as a result of using inline specifier when defining functions in .h files:

  • Is the code expansion generated by inline still lesser than what would be caused by static inline?

  • Why is there a need for an extern inline declaration in the corresponding .c file ?

Ulfalizer
  • 4,664
  • 1
  • 21
  • 30
thegeeklife
  • 107
  • 6
  • If you're worried that your inline functions might not be inlined, maybe you shouldn't be inlining them in the first place? If you're not worried, then the compiler will be inlining the code anyway, and the code expansion (which presumably should get you a speed benefit; if it doesn't, there wasn't any point inlining the function in the first place) should be acceptable. Don't use `inline` willy-nilly. Use it with caution and judgement, and in case of doubt, don't use it. See also [Is `inline` without `static` or `extern` ever useful in C99?](http://stackoverflow.com/questions/6312597) – Jonathan Leffler Mar 11 '15 at 23:55
  • The reason for using inline in my code is basically for allowing function definition in the header file. Without an inline / static inline, compiler complains of multiple symbol definitions. I am a tad bit worried about code size, but mainly i"m trying to see which way gives me more benefit i.e less code expansion- inline or static line. – thegeeklife Mar 12 '15 at 00:39
  • @thegeeklife: Plain `inline` (along with `extern inline` in some `.c` file) would be the better of those two options. It means you're creating a single logical function instead of many, as outlined in my answer. You should consider whether you really need code in headers though. It's simpler to have code in `.c` files, plus you don't risk nudging the compiler into inlining stuff it otherwise might not have (which seems to be what you're worried about, as Jonathan pointed out). The `.c` file where you'd put the `extern inline` would probably be a logical place to put the definition instead. – Ulfalizer Mar 12 '15 at 05:02
  • Also note that the compiler can inline a call to a function from a different translation unit (`.c` file) without having to use `inline` if you use link-time optimization. – Ulfalizer Mar 12 '15 at 05:04

1 Answers1

6

It could generate smaller code. The reason is that inline (as opposed to static inline) will give the function external linkage so that all calls to the function from different translation units refer to the same logical function. With static inline, each translation unit will get a unique instance of the function instead, which could increase code size if the compiler chooses not to inline. (It's also cleaner codewise to not have multiple identical functions.)

The reason you need extern somewhere is because it makes the compiler generate an external definition of the function that can be called from other translation units. Without extern, no such instance is generated. The no-extern case differs from internal linkage in that the inline definition only provides an "alternative" to the external definition of the function. An external definition must still exist (i.e., some translation function must use extern to generate an external definition), and the compiler is free to use it instead of it wants to.

Here's some relevant standardese (for C11: ISO/IEC 9899:2011 §6.7.4 Function specifiers, ¶7):

Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit. If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition.140)

140) Since an inline definition is distinct from the corresponding external definition and from any other corresponding inline definitions in other translation units, all corresponding objects with static storage duration are also distinct in each of the definitions.

By the way, inline IMO often isn't worthwhile (as a hint -- the compiler is still free to not inline) compared to simply letting the compiler choose when to inline purely on its own. For modern compilers that support link-time optimization, the compiler can even inline functions across translation units if you pass the right flags (e.g., -flto in GCC).

Ulfalizer
  • 4,664
  • 1
  • 21
  • 30