Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

gcc - Requirements to use flto

If I want to compile my project with -flto is it enough to have built gcc with --enable-gold or do I also need to build gold and replace ld with it? And do I need any other flags? Ie I'm doing this

gcc -flto one.c two.c
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to https://gcc.gnu.org/wiki/LinkTimeOptimization#Requirements,

Despite the "link time" name, LTO does not need to use any special linker features. The basic mechanism needed is the detection of GIMPLE sections inside object files. This is currently implemented in collect2. Therefore, LTO will work on any linker already supported by GCC.

Furthermore, the GCC documentation for -fuse-linker-plugin says:

This option is enabled by default when LTO support in GCC is enabled and GCC was configured for use with a linker supporting plugins (GNU ld 2.21 or newer or gold).

So you don't need gold at all, even if you want to use the special "linker plugin" feature to pick up optimization information from object files in library archives.


There are usage examples in the -flto documentation. Either

      gcc -o myprog -flto -O2 foo.c bar.c

or

      gcc -c -O2 -flto foo.c
      gcc -c -O2 -flto bar.c
      gcc -o myprog -flto -O2 foo.o bar.o

will work.


As of GCC 4.9, you don't even need -flto for linking:

The only important thing to keep in mind is that to enable link-time optimizations you need to use the GCC driver to perform the link-step. GCC then automatically performs link-time optimization if any of the objects involved were compiled with the -flto.

And as of GCC 5:

Contrary to earlier GCC releases, the optimization and target options passed on the link command line are ignored.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...