How can I use lto with static libraries?

The answer, as I found out from this post by GCC developer Honza Hubička, is to use the gcc-ar wrapper instead of ar by itself:

$ gcc-ar rcs library.a library.o

This invokes ar with the right plugin arguments, in my case were

--plugin /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/liblto_plugin.so

As a complimentary answer: with GCC there is also the possibility to use -ffat-lto-objects which adds classic object code to the files in the archive. This makes it possible to use the static library in code that you build without -flto.

Tags:

C++

Gcc

Lto