gcc 链接时可以使用多核吗?
因此,当使用 GCC 编译大量源文件时,可以使用 -j 来使用所有可用的内核。但是链接器呢?是否有类似的选项来加速链接或者 GCC 不支持多线程?在一些较大的项目中,它确实需要一段时间...(...而且我讨厌等待!)
编辑:感谢您指出 -j 是 make 而不是 gcc/g++ 的选项。但这并不能回答我的问题!我想知道 gcc 是否可以在链接程序时使用多线程!
So when compiling tons of source files with GCC one can use -j to use all available cores. But what about the linker? Is there a similar option to speed up linking or does GCC not support multi-threading? In some larger projects it can really take a while ... (... and I hate to wait!)
Edit: Thanks for pointing out that -j is a option for make and not gcc/g++. But this does not answer my question! I would like to know if gcc can use multi threading while linking a program!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试 gold,它是由 Ian Lance Taylor 等人开发的。来自 Google 并作为 GNU binutils 包的一部分发布。
来自维基百科:
我必须承认我自己还没有尝试过,但它在 WebKitGTK 项目 网页。
有关详细信息,请参阅 gold 作者撰写的文章:A New ELF Linker 。
更重要的是,请参阅 Sander Mathijs van Veen 题为 Concurrent Linking with the GNU Gold 的关于增量/并行/并发链接的工作链接器及其参考书目。
Try gold, which was developed by Ian Lance Taylor et al. from Google and released as part of GNU binutils package.
From Wikipedia:
I must admit I haven't tried it myself yet but it's mentioned on WebKitGTK project webpage.
For more information see an article written by the author of gold: A New ELF Linker.
More importantly, see the work on incremental / parallel / concurrent linking by Sander Mathijs van Veen titled Concurrent Linking with the GNU Gold Linker and the bibliography therein.
LLVM 项目开发的链接器 lld 默认情况下将使用多核。我还发现它比运行多线程的 gold 快大约 2 倍 (
-Wl,--threads -Wl,--thread-count,xxx
)lld, the linker developed by the LLVM project, will use multiple cores by default. I have also found it to be about 2x faster than gold running with multiple threads (
-Wl,--threads -Wl,--thread-count,xxx
)2022 年,mold 链接器是另一个不错的选择。
它是可用于 ELF 目标的最快链接器,并且具有最积极的并行性。
In 2022 the mold linker is another great option.
It is the fastest linker available for ELF targets and has the most aggressive parallelism.
您所指的
-j
选项是由make
而不是gcc
处理的。使用
make -j n
要求make
通过多个并行进程运行Makefile
中的操作(将n
替换为对于make -j 2
来说,它是2
进程)。在进行并行构建时,Make 将很好地处理大多数同步任务。
The
-j
option you are referring to is handled bymake
notgcc
.Using
make -j n
asksmake
to run the actions in theMakefile
with multiple parallel process (Replacen
with a number. In the case ofmake -j 2
it's2
process).Make will handle most synchronization tasks well when doing parallel builds.
在用gold替换ld - 有经验吗?我已经进行了基准测试在最小综合基准上比较 LD vs gold vs LLVM LLD,时间结果清楚地表明 gold 和 LLVM LLD 都能够根据
时间
进行并行链接,例如:我们知道使用了并行链接,因为
在这两个 CPU 中,wall
时间都小于用户
时间这些基准测试还再现了 Martin Richtarsky 的 2 倍更快的 LLVM LLD 链接时间。
At Replacing ld with gold - any experience? I have benchmarked LD vs gold vs LLVM LLD on a minimal synthetic benchmark, and the time results clearly show that both gold and LLVM LLD are capable of parallel linking according to
time
, e.g.:We know that parallel link was used because the
wall
time was smaller than theuser
time in both of those CPUsThose benchmarks also reproduce Martin Richtarsky's 2x faster LLVM LLD link times.
以下是 CMake 上的两个选项(在 3.16.3 上测试):
更完整的示例:此处
注意:如果您收到错误消息“ld. lld:错误:无法找到库...”您可能需要在 link_directories 调用中添加库目录列表,如下所示:
Here are two options for those on CMake (tested on 3.16.3):
More complete example: here
Note: if you receive the error message "ld.lld: error: unable to find library ..." you may need to add the list of library directories in a link_directories call like so: