1

I've seen a few questions addressing this general topic, but I'm still unsure exactly what the correct approach is for ISO C, and how that may or may not vary from GNU C.

If I have some function inline void foo() in file.h defined with the inline keyword, should I also declare that function in file.c as extern void foo();? Assume that multiple .c files will include file.h.

From what I've read the answer seems to be "yes" and have something to do with how the compiler looks for definitions emitted by other translation units, but to be honest I don't fully understand the implications.

I'm working on a project right now that has a lot of functions declared inline within the header files, and none of those functions are declared in the corresponding .c files. Everything compiles without gcc complaining, but is this approach actually correct?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Evan
  • 603
  • 1
  • 11
  • 18
  • `in file.h defined` `should I also declare` Are you mixing terms "function definition" and "function declaration"? `but is this approach actually correct?` are those function defined or declared? Are they `static`? – KamilCuk Jun 19 '20 at 17:10
  • 1
    Please see the related question [Inline functions and external linkage](https://stackoverflow.com/questions/16386256/inline-functions-and-external-linkage). – Weather Vane Jun 19 '20 at 17:14
  • My understanding is that we need a declaration in the `.c` file to force emission of the function symbol in at least one TU. Without this no symbol for the function will ever be emitted. – Evan Jun 19 '20 at 17:20

2 Answers2

4

Yes, if you use inline without static, there needs to be an actual external definition of the function somewhere in case the compiler declines to inline it in one or more places. The canonical way to do that is to make a source file containining (using your example names) nothing but:

#include "file.h"
extern void foo();

Personally, I find extern inline semantics in C confusing and messy, and prefer to avoid them entirely by making all inline functions static. Of course this wastes space with multiple instantiations of the function if the compiler declines to inline, but you should not be doing this with functions of nontrivial size anyway.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Thanks! Aside from potentially wasting space if the compiler doesn't inline, does using static also have the side effect of making making it impossible to compare function pointers to the function from different translation units? – Evan Jun 19 '20 at 21:11
  • @Evan: Yes, it makes the function pointers non-equal because they're different functions. – R.. GitHub STOP HELPING ICE Jun 19 '20 at 21:43
  • if you inline a function (I mean, if the compiler inlines it) you waste space anyway, because you have a copy of the function code at every call point. – Luis Colorado Jun 27 '20 at 19:49
  • @Luis: The function body is just a function call, which you already have if the function is out-of-line. – R.. GitHub STOP HELPING ICE Jun 27 '20 at 20:31
  • @R..GitHubSTOPHELPINGICE, but the compiler can optimize it to avoid the unused paths, the body is not the same, as you don't use a new stack frame, so you avoid the preamble and postamble of the function as such. Inline code is good for simple functions on which the time spent in building a stack frame and entering and returning code is significant in the timing of the call. – Luis Colorado Jun 27 '20 at 21:22
  • @Luis: we are talking about a particular function in OP's question. Not a broader topic. – R.. GitHub STOP HELPING ICE Jun 28 '20 at 00:42
  • An inline function called `foo()` ??? actually? Is that a concrete function and you are not talking genericly? My apologies then, I misunderstood clearly the intention when I saw `foo()`'s name. – Luis Colorado Jun 30 '20 at 14:50
  • 1
    @LuisColorado: Sorry, I thought this comment thread was on a different but similar question. Nonetheless, whether to make the function inline is not the question here. The question is just how to do it correctly. So I don't think our discussion is on-topic. – R.. GitHub STOP HELPING ICE Jun 30 '20 at 20:46
-1

inline means the compiler will expand that function where it is used in the code (instead of calling it as a function). That means that an inline definition in a header file with the function implementation in a .c file makes no sense, as wherever you include the header in another .c file the function can't be expanded inline as its code implementation is not known.

So you should keep the inline function and its code implementation in the header file.

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40