16

I have a static inline function defined in an H file, and at one point in a C file, I'm assigning a pointer to the function, something like this:

foo.h:

static inline void frobnicate(void) {
    // frobs something.
}

foo.c

#include "foo.h"

void execute(void (*func)(void) ) {
    func();
}

void blahBlahBlah(void) {
    execute(frobnicate);
}

bar.c

#include "foo.h"
// ...
frobnicate();

So I think what will happen here is that the compiler will inline the call to frobnicate from bar.c, but in foo.c, it will actually have to create a function to implement frobnicate, so that it can have a working pointer to it.

Can anyone confirm if my understanding is accurate, and correct me otherwise?

brianmearns
  • 9,581
  • 10
  • 52
  • 79
  • 3
    *You* can confirm it, by reading the generated code. I don't think there's a specified way in which this *has* to work. I wouldn't be surprised if the compiler goes "someone wants the address of this, so let's not inline it, then". – unwind Jan 16 '12 at 20:10
  • 7
    Remember that `inline` is only a _suggestion_ to the compiler – Seth Carnegie Jan 16 '12 at 20:10
  • It can as well infer it could simply directly inline `frobnicate` into `blahBlahBlah` and skip the middlemen. No pointer to `frobnicate` is stored in a way that would allow it to be used form another translation unit so `frobnicate` doesn't need to exist in the assembly output. – Petr Skocik Dec 04 '18 at 19:07

2 Answers2

11

inline is one of the misnomers of the C standard. Its main meaning is to be able to put the definition of a function in a header file without having to deal with "multiple definition" problems at link time.

The official way in C99 and C11 to do what you want to achieve is to have the inline definition in the header file, without the static. Since you also need the symbol to be emitted you need to tell the compiler in which compilation unit this should be. Such an instantiation can be done by have a declaration in that .c file where you omit the inline keyword.

Most naturally you could use the .c file where you actually need the symbol.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • If the inline definition is in the header file without `static`, then wouldn't I have multiple definitions of the same name (the inline function) when I try to link? – brianmearns Jan 16 '12 at 20:29
  • You will have linkage problems. Either make it `static inline`, and risk multiple definitions, or give up inline. You can also make a wrapper, which will be in a C file and won't be inline, and take a pointer to it (no performance hit, because the real function will be inlined into the wrapper). – ugoren Jan 16 '12 at 21:38
  • 1
    @bmearns, with a C99 compliant compiler you will have no linkage problem. This is exactly the sense of the `inline` keyword. http://gustedt.wordpress.com/2010/11/29/myth-and-reality-about-inline-in-c99/ – Jens Gustedt Jan 17 '12 at 10:48
  • @Jens: Thanks, that link clarified it quite a bit. I'm not exactly clear on how it's handled by the compiler, but I think I understand how to apply it. – brianmearns Jan 17 '12 at 12:23
10

Yes, you are right. When you take the pointer to the function the compiler must create an "stand alone" version where the code can be called as a normal function.

The benefit of inlining a function is that the calling code need not to be created and any other optimization can be aplied to integrate both the caller function and the inlined function. But when you need to do a regular call to the function(as when you take the address to call it latter), those optimizations are not possible anymore.

Governa
  • 908
  • 10
  • 21
  • 2
    This may be true for many compilers, but I cant imagine why a compiler would be required to do so. The OP's example could be inlined quite easily even in the case of the function pointer. –  Jan 16 '12 at 20:31
  • `inline` is a recommendation. If the compiler can't inline, it won't, and if it can't, it might still decide not to (and it may inline even without the `inline` keyword). Function pointers generally prevent inlining, though in this case the compiler may figure out that they don't (or it might not bother). – ugoren Jan 16 '12 at 21:35
  • Yes, in this case it would be possible to inline the function. I assumed this was an simplified version of the problem. But you are right. – Governa Jan 17 '12 at 13:06