gcc - 使用 flto 的要求

标签 gcc ld binutils lto gold-linker

如果我想用 -flto 编译我的项目用 --enable-gold 构建 gcc 就够了吗?或者我还需要建立黄金并用它替换 ld 吗?我还需要其他标志吗?即我正在做这个

gcc -flto one.c two.c

最佳答案

根据 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.



此外,GCC documentation-fuse-linker-plugin说:

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).



所以你不需要gold即使您想使用特殊的“链接器插件”功能从库文件中的目标文件中获取优化信息。

-flto documentation中有使用示例.任何一个
      gcc -o myprog -flto -O2 foo.c bar.c

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

将工作。

截至 GCC 4.9 ,你甚至不需要-flto用于链接:

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.



以及截至 GCC 5 :

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

关于gcc - 使用 flto 的要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31688069/

相关文章:

c++ - 使用 GCC : Get Call Count of Functions in a Shared Library 进行分析

gcc - ld 脚本中节定义末尾的 =0 是什么意思?

riscv - 向 RISCV-32ima : "bad RISCV-opcode" 添加新指令

dynamic - 如何仅将动态符号保留在共享对象中以进行动态链接?

c++ - 为什么 gcc8.3 似乎试图编译未使用的模板函数?

c - 这种 union 的使用是否严格符合要求?

c++ - 某些平台上的变量初始化并非随处可见

gcc - 我不明白-Wl,-rpath -Wl,

static-libraries - gnu ld 链接整个目标文件还是只链接所需的函数?

android - binutils-gold 支持arm/Android 吗?