Question

I find many resources online harping about how inline (and even __attribute__((always_inline)) or __forceinline) does not force the compiler (e.g. gcc or VisualC++) to inline the function. But when exactly will inline not be enforced? Is there a toy example?

Perhaps not necessarily the same question, when will a function tagged with inline included in two different cpp files create problems during linkage? Namely, generate duplicate symbols?

Here's a concrete sandbox for trying to break the compiler inlining and generate a duplicate symbol:

In myinline.h:

inline int myinline()
{
  // code that cannot be inlined...
  ...
}

In aux.cpp:

#include "myinline.h"
int aux()
{
  return my_inline();
}

In main.cpp:

#include "myinline.h"
int aux();
int main()
{
  return aux() + my_inline();
}

Then, e.g. in the case of gcc is there some (minimal) code for myinline that will cause a duplicate symbol when compiling and linking with:

g++ -o aux.o -c aux.cpp
g++ -o main.o -c main.cpp
g++ -o example aux.o main.o

?

Was it helpful?

Solution

"Inlining" and "duplicate symbols" are different things. The inline keywords explicitly allows multiple definitions (i.e. it exempts you from the one-definition rule), so the platform (compiler and linker) must know how to handle and deduplicate that.

(This happens all the time for class member functions that are defined in headers.)

If you just want code generation to happen, you can store the address of the function somewhere:

auto fp = my_inline;

That way the compiler must generate a definition of the function, so as to be able to give it an address. But even if you do this in every translation unit, you won't get a linker error, as this will be deduplicated at link time. The requirement that all definitions be identical makes sure that this is well-defined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top