What does GCC's -flto do at compile and link time, and what breaks with static libraries or mixed GCC versions?
answer
- optimize again at link time
- GIMPLE in ELF sections
- -flto at compile and link
- slim objects need gcc-ar
- bytecode tied to one GCC
basics
~20 s-flto stores GCC's intermediate representation in each object and optimizes the whole program again at link time, so inlining crosses files. Archives of such objects need gcc-ar, and the bytecode only works with the GCC version that wrote it.
solid answer
~50 sWith `-flto`, compiling writes **GIMPLE bytecode**, GCC's intermediate representation, into special ELF sections of each object. At link time the GCC driver, through the linker plugin, reads those sections, merges them as if they were one translation unit and optimizes again: functions inline across files, unused code disappears, interprocedural constant propagation sees every caller. Pass `-flto` and the optimization options at both compile and link, link with the GCC driver, and use `-flto=auto` to parallelize. Where the linker plugin is available the default is **slim** objects that hold only bytecode, so plain `ar` cannot index them: archives need `gcc-ar` and `gcc-ranlib`, or `-ffat-lto-objects` to add normal code as well. The bytecode is version-checked, so objects from one GCC release do not work with another. LTO also turns on `-Wodr`, which reports C++ One Definition Rule violations across files.
code
bash · 6 linesg++ -O2 -flto -c geo.cpp mathx.cpp # slim objects: GIMPLE bytecode only
gcc-ar rcs libgeo.a geo.o mathx.o # plugin-aware ar writes a usable index
g++ -O2 -flto=auto main.cpp -L. -lgeo -o app # whole-program optimization at link time
# a library also consumable without LTO:
g++ -O2 -flto -ffat-lto-objects -c geo.cpp mathx.cppgo deeper
Know that -flto lets the compiler optimize across source files at link time.
Explain that objects carry GIMPLE bytecode, that -flto and -O go on both compile and link lines, and why archives need gcc-ar.
Handle slim versus fat objects, version lock-in and parallel links, and treat new -Wodr warnings as real ODR bugs.
Decide where LTO is worth its link-time cost, and forbid shipping slim LTO artifacts across compiler versions.
## What changes with -flto Normally each translation unit is optimized alone, and the linker only stitches finished machine code together. A small function defined in `geo.cpp` cannot be inlined into `main.cpp`. **Link-time optimization** moves part of the optimizer to the link: 1. **Compile**: `g++ -O2 -flto -c geo.cpp` writes GIMPLE bytecode, GCC's intermediate representation, into special ELF sections of `geo.o`. 2. **Link**: `g++ -O2 -flto main.o geo.o -o app` reads the bytecode of every object, merges it into one image and runs interprocedural optimization and code generation over all of it. GCC's manual gives the headline consequence: the inliner can inline functions from one object into another, because all interprocedural analyses work across the files as if they were one. Knowing, through the linker plugin, which symbols are used from outside the program also lets the optimizer drop or specialise code nothing else calls. The manual's advice is to compile all participating files with the same options and to repeat them at link time; an `-O` level given **only** at link time is generally ineffective, because early passes run at compile time. ## Making it work in a build - Link with the **GCC driver** (`g++`), not by calling `ld` directly. - `-flto=auto` parallelizes the link-time work using a running make job server, or else the number of CPU threads; `-flto=n` fixes the job count. - `-fno-lto` at the link disables it even if objects carry bytecode. - `-g` must be given at compile time for the linked program to carry debug information. - GCC 16 also documents `-flto-incremental=path`, a cache that can shorten edit-compile cycles with LTO. ## Slim and fat objects | Object kind | Contains | Usable by a non-LTO link? | |---|---|---| | slim (default where the linker plugin is supported) | GIMPLE bytecode only | **no** | | fat (`-ffat-lto-objects`) | bytecode **and** ordinary machine code | yes | Slim objects compile faster, but every tool must understand them. A static library is the usual casualty: archiving slim objects with plain `ar` produces an archive whose index cannot list the symbols, so links fail with undefined references. GCC provides **`gcc-ar`, `gcc-nm` and `gcc-ranlib`**, wrappers that load the plugin; installing the plugin into binutils' plugin directory has the same effect. A library meant for both LTO and non-LTO consumers should be compiled with `-flto -ffat-lto-objects`. ## Version lock-in The manual is blunt: LTO bytecode is versioned with a strict version check, so files generated by one version of GCC **do not work with an older or newer version**. Practical consequences: - Never ship a static library of slim LTO objects to people who may use another GCC. Ship ordinary objects compiled without `-flto`; GCC performs LTO automatically when an input carries bytecode, so a consumer with a different GCC would have to link even fat objects with `-fno-lto`. - A CI cache of LTO objects must be keyed on the exact compiler version. ## LTO and the One Definition Rule Merging every translation unit exposes conflicts separate compilation hides. If two files define the same class differently, for example because a macro changed a member, the program already violates the ODR. With LTO, GCC can see both definitions and **`-Wodr`**, enabled by default, warns about the mismatch. The language rule itself belongs to the compilation model; the practical point is that turning on LTO often surfaces real latent bugs, and they should be fixed rather than silenced. ## What it costs - **Link time and memory.** The link step now runs the optimizer and code generation for the whole program. `-flto=auto` spreads it over cores, and `-flto-partition` controls how the work is split; the default is `balanced`. - **One compiler for the whole link.** Every object carrying bytecode must come from the same GCC release, which constrains prebuilt dependencies and caches. - **Harder debugging of the optimizer's decisions.** Code from several source files may be inlined into one function, so a profile or a crash backtrace can attribute time to a caller in another file. - **Hidden behaviour changes.** Code that relied on undefined behaviour or on an ODR violation, and happened to work while files were optimized separately, may change behaviour once the optimizer sees everything. ## When it pays off - Code with many small functions spread across files. - Programs linked from static libraries built with the same compiler. - Release builds, where longer links are acceptable. It pays less when most time is spent in already-optimized shared libraries, which LTO cannot see into.
- Why does an LTO static library built with plain ar cause undefined references?Slim LTO objects contain only bytecode, and plain `ar` without the plugin cannot read their symbols, so the archive index is empty or incomplete. The linker finds nothing to extract. Rebuild the archive with `gcc-ar`, or compile with `-ffat-lto-objects`.
- Should you silence -Wodr warnings that appear after enabling LTO?No. They report that two translation units define the same entity differently, which is already undefined behaviour without LTO; LTO merely makes it visible. Find the differing definitions, often caused by a macro or packing setting that differs between files, and make them identical.
- -flto only needs to be passed at link time.
- LTO objects from any GCC version can be mixed in one link.
- Plain ar works for slim LTO objects exactly as for normal ones.
- ODR warnings under LTO are false positives caused by the optimizer.
- LTO can inline code from system shared libraries into the program.
Assumes GCC 16: slim objects are the default where the linker plugin is supported, and -flto-incremental is documented in this release.