Using link-time optimizer (-flto) with static library

1 post / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 3

Hi everyone,


I just had a hard time getting LTO work with a static library in AS (7.0.1931) ...


I got one solution with two projects, one static library and one executable (native C toolset for AVR8 (GCC == 5.4.0), target is a Mega328PB).

Both setup to use LTO (-flto switch for compiler and linker!).


Issue was that although the static library was successfully built, when linking the executable, the linker could not resolve any symbols from the library ...

Even more puzzling: using avr-objdump i could see that the symbols where actually present in a symtab section (.gnu.lto_.symtab.c3bda65c) in the library!


Doing some research a found this

Basically says one has to either use gcc-ar instead of plain ar (so avr-gcc-ar instead of avr-ar for us) OR explicitly tell ar to use a LTO plugin DLL/SO.


Looks like AS is calling avr-ar and i was not able to find any (obvious) way to make it call avr-gcc-ar instead ...


To make a long story short, after having a look at avr-gcc-ar in a Hex editor and some searching in the avr8-gnu-toolchain directory in my AS installation, i found the following the solve this (well, at least, is does link now ...):


  1. Copy "liblto_plugin-0.dll" from
    "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\libexec\gcc\avr\5.4.0" to
    "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin"
    (fix up path to match YOUR AS installation location if needed!)
  2. Add "--plugin liblto_plugin-0.dll" to the "AVR/GNU Archiver" flags in your static library project.


Hope this might help others who try to make LTO work with static libraries in AS!