Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Inline functions and link time code generation

Tags:

People also ask

What is Link Time Code Generation?

Link-time code generation (LTCG) enables cross source-file optimization by delaying code generation until the link stage. This can significantly reduce code size. To enable LTCG, compile your source with -c --ltcg to create objects in an intermediate format.

Is inline compile time?

Whether something is inline or not is part of code generation. That's the compiler's job. Ergo, inlining is done at compile time.

Can the linker inline functions?

The linker can inline small functions in place of a branch instruction to that function. For the linker to be able to do this, the function (without the return instruction) must fit in the four bytes of the branch instruction.

What is inline function in C programming?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.


Up till a while a ago my code base was very close to #include hell. Every time I changed an even mildly important .h file practically all the files got recompiled.
The main reason for such high header dependency was that I have many small functions that need to be inline and I was under the impression that for inline to work they need to be in the same translation unit as the calling code, so they need to be in the header. For the inline function to even compile other headers need to be included in the header as well, ad infimum.

Enter link-time code generation (in Visual Studio). One of the main stated advantages of this is that now inline function can cross translation units.
But I'm still iffy. How can I really be sure that these functions really get inlined? I realize that the compiler can basically do whatever the hell it wants no matter where I define the function.

Is there a way to check what gets inlined?