GCC v12.1 关于串行编译的警告

GCC v12.1 warning about serial compilation

我今天(2022 年 5 月 12 日)升级了整个 arch linux 系统。 gcc 也从 v11.2 升级到 v12.1。我尝试通过以下命令使用 g++gcc 编译器集合的一部分)编译我的一些程序:

g++ -O3 -DNDEBUG -Os -Ofast -Og -s -march=native -flto -funroll-all-loops -std=c++20 main.cc -o ./main

程序编译完美,运行 没有任何错误,但我收到警告:

lto-wrapper: warning: using serial compilation of 2 LTRANS jobs

但是,当使用 v11.2 编译同一个程序时,它 产生零个错误和警告

我的问题:

这是我机器上的 g++ 配置:

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (GCC) 

显然这是 -flto 选项的最新更改。通过 google 搜索中的一些变化,我能够找到 this mail conversation:

Likewise if people just use -flto and auto-detection finds nothing:

warning: using serial compilation of N LTRANS jobs note: refer to http://.... for how to use parallel compile

[...]

That is, teach users rather than second-guessing and eventually blowing things up. IMHO only the jobserver mode is safe to automatically use.

所以这是关于正确使用 -flto 选项。我无法在我的系统上轻松获得 GCC 12,因此我自己无法尝试,但您可以尝试 -flto=1-flto=auto 来消除警告。

不管怎么说,这个警告似乎是无害的。它只是告诉您 GCC 使用 2 个并行线程来进行 link 时间优化。

-flto 的确切语义和效果(连同其他优化选项)在 GCC manual 中有详细描述。顺便说一下,您不应该像在命令行中那样发送垃圾邮件优化选项。例如,指定多个 -O... 选项只会产生其中最后一个的效果。除非你确切地知道你在做什么并且已经仔细阅读了手册,否则只要坚持使用-O3就可以了。