gcc - 何时对多个共享库使用某些优化,例如 -fwhole-program 和 -fprofile-generate

标签 gcc compiler-optimization compiler-flags

可能是一个简单的答案;我对其中一些标志的 GCC 文档中使用的语言感到非常困惑!

无论如何,我有三个库和一个使用所有这三个库的程序。我使用单独的(可能)不同的警告标志集分别编译我的每个库。但是,我使用相同的优化标志集编译所有三个库。

然后,我使用自己的一组警告标志和库编译期间使用的相同优化标志来编译我在这三个库中链接的主程序。

1)我是否必须编译带有优化标志的库,或者我可以在编译最终程序并链接到库时使用这些标志吗?如果是后者,那么它会优化这些库中的所有代码还是仅优化一些(大概是被调用的)代码?

2)我想使用 -fwhole-program -flto -fuse-linker-plugin 和链接器插件黄金。我在哪个阶段使用这些进行编译......只是最终编译还是在编译库期间需要存在这些标志?

3) 与 2) 几乎相同,但是使用 -fprofile-generate -fprofile-arcs 和 -fprofile-use。我理解一个人首先使用生成运行程序,然后使用使用。但是,我是否必须使用生成/使用等或仅编译最终程序来编译每个库?如果它只是最后一个程序,那么当我使用 -fprofile-use 进行编译时,它还会优化库功能吗?

最佳答案

I then compile my main programme linking in these three libraries with its own set of warning flags and the same optimisation flags used during the libraries' compilation.



这里有两个步骤,不是一个。编译主程序以生成目标文件是第一步,然后将所有内容链接在一起以生成可执行二进制文件是第二步。

1) Do I have to compile the libraries with optimisation flags present or can I just use these flags when compiling the final program and linking to the libraries? If the latter, will it then optimise all or just some (presumably that which is called) of the code in these libraries?



必须为每个源代码文件指定优化标志和任何其他编译器级别的标志。每次编译源文件以生成目标文件时,都需要指定标志。然后,链接器使用不同的链接器标志集来生成库或可执行文件。

2) I would like to use -fwhole-program -flto -fuse-linker-plugin and the linker plugin gold. At which stage do I compile with these on ... just the final compilation or do these flags need to be present during the compilation of the libraries?



您必须指定 -flto在编译每个源代码文件(或那些可用的)时。您不需要指定 -fuse-linker-plugin-fwhole-program使用黄金链接器或 GNU ld 2.21 或更新版本时。有关更多信息,请参阅 documentation-flto .

3) Pretty much the same as 2) however with, -fprofile-generate -fprofile-arcs and -fprofile-use. I understand one first runs a programme with generate, and then with use. However, do I have to compile each of the libraries with generate/use etc. or just the final programme? And if it is just the last program, when I then compile with -fprofile-use will it also optimise the libraries functionality?



与 2 相同。需要为每个源代码文件指定这些标志,以便优化所有代码。

请注意,如果您没有源代码,您仍然可以链接未使用相同标志优化的静态或动态库,并且代码将起作用。也就是说,您可以混合在不同级别优化的代码。

关于gcc - 何时对多个共享库使用某些优化,例如 -fwhole-program 和 -fprofile-generate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13674894/

相关文章:

c - 无法在不丢失预处理器的情况下在 C 中包含 ASM 头文件

c - 评估 sizeof 导致性能改进 C

c++ - -fno-exceptions 对于某些库?

c - GCC -c 选项的 CMake 等价物是什么?

gcc - 为什么编译器错误会成批出现?

c - 静态链接库之间共享的地址被视为 32 位而不是 64 位

c# - 我的编译器会忽略无用的代码吗?

c++ - 为什么 std::fill(0) 比 std::fill(1) 慢?

kotlin - @JvmDefault 以及如何添加编译器选项

compiler-flags - 如何将全局 CXX 编译器标志添加到 yocto 构建