1

Given a C++ program that uses GNU autotools, what's the easiest way to compile it with -flto (link time optimization)? My understanding is that it is customary on Unix for such optimization flags to be specified by the user or packager, not by the programmer.

rwallace
  • 31,405
  • 40
  • 123
  • 242

1 Answers1

4

According to this post, the -flto flag needs to be passed as a compilation flag and as a linker flag, so:

./configure CXXFLAGS="-flto" LDFLAGS="-flto" ...

or possibly:

./configure CXXFLAGS="-flto" LDFLAGS="-Wc,-flto" ...

might work.

ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • 2
    Or alternatively `./configure CXX='g++ -flto -O2'` since *optimizations* should be *same* at *compile* and at *link* time – Basile Starynkevitch Dec 03 '14 at 05:42
  • That works, thanks! Actually, `CXXFLAGS='-O3 -flto'` seems to suffice. – rwallace Dec 04 '14 at 11:41
  • Lucky for you that it was that simple! I needed to add a pile of other stuff, as hinted by this answer: http://stackoverflow.com/questions/25878407/how-can-i-use-lto-with-static-libraries#comment67660913_25878408 The final configure command was this: `./configure --prefix=/opt/testmake AR=gcc-ar RANLIB=gcc-ranlib CXXFLAGS='-O3 -flto'` – underscore_d Oct 22 '16 at 22:12