Breaking the Translation Unit Barrier with GCC Link-Time Optimization
Learn how to use GCC's Link-Time Optimization (-flto) to enable cross-module inlining and dead-code elimination, reducing binary size and improving performance.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Learn how to use GCC's Link-Time Optimization (-flto) to enable cross-module inlining and dead-code elimination, reducing binary size and improving performance.
Symptom When compiling with -flto and -fcommon , GCC often succeeds linking multiple source files that each declare a tentative global variable. Switching to -fno-common triggers duplicate symbol errors during the LTO phase, even though standard linking without LTO honours the flags consistently. Uncertainty The GCC manual does not explicitly state whether -
Goal: Assess whether GCC 12 could enable link‑time optimization by default for static libraries when the -fuse-linker-plugin flag is present, similar to the behavior already applied to shared libraries. Constraints: The documentation explicitly states that static libraries still require -flto to activate LTO, and the change to shared‑library defaults does no
Goal Ensure that any change to a source file that is part of an LTO module is reflected in the final binary, without relying on manual cache deletion. Constraints & Uncertainty LTO builds a cache keyed by the module’s bitcode hash and the compiler flags used at link time. When a source file changes but the bitcode hash or flags remain the same, the cache
Link-Time Optimization (LTO) in GCC relies on the lto1 backend and a corresponding plugin for the GNU Binutils linker. During the compilation phase, translation units are stored as GIMPLE intermediate representation rather than standard machine code, deferring final code generation to the link stage. A critical constraint in this integration is the stability
Goal Determine whether GCC -flto object files can be archived with the host system's ar and later linked with ld without the GCC LTO plugin, or whether gcc-ar and gcc-ranlib are required for correct symbol-table indexing at the compiler-to-linker boundary. Constraints and uncertainty GCC -flto translation units contain GIMPLE intermediate representation rath