Enable LTO and check if LTO is used

5.6k Views Asked by At

I have been trying to enable LTO on some code I have, and am trying to figure out the instructions to do it. GCC is what I am using

I see some instructions mentioned here: https://johnysswlab.com/link-time-optimizations-new-way-to-do-compiler-optimizations/

This is basically the set in the link above: To enable LTO, follow these simple steps:

Add option -flto to the invocation of compiler. Add option -flto to the invocation of the linker. Additionally, you need to add all options from the compiler invocations to the invocation of the linker. So if you called your compiler with “-march=i486 -O3 -fno-stack-protector“, you will need to pass the same options to the linker. Now you compile your program as regular. Unless you are using a very old version of the compiler, you shouldn’t expect any problems here.

I see this as well which talks about AR being switched to gcc-ar as an example:

How to write LTO-enabled code?

Questions:

  1. Is the set of instructions in this link enough? https://johnysswlab.com/link-time-optimizations-new-way-to-do-compiler-optimizations/ or is there something additional I need to do? Same for static and dynamic library compiles?

  2. How do I verify after my final build that LTO indeed was used?

1

There are 1 best solutions below

10
On

Is the set of instructions in this link enough?

Yes.

is there something additional I need to do?

No.

Same for static and dynamic library compiles?

Yes.

How do I verify after my final build that LTO indeed was used?

How to detect code compiled with LTO?

Clang: How to check if LTO was performed