gcc 链接时可以使用多核吗?

发布于 2024-10-19 17:03:35 字数 223 浏览 1 评论 0原文

因此,当使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ㄟ。诗瑗 2024-10-26 17:03:35

尝试 gold,它是由 Ian Lance Taylor 等人开发的。来自 Google 并作为 GNU binutils 包的一部分发布。

来自维基百科:

编写 gold 的动机是制作一个比 GNU 链接器更快的链接器,特别是对于用 C++ 编码的大型应用程序

我必须承认我自己还没有尝试过,但它在 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:

The motivation for writing gold was to make a linker that is faster than the GNU linker, especially for large applications coded in C++

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.

○愚か者の日 2024-10-26 17:03:35

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)

情话墙 2024-10-26 17:03:35

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.

2024-10-26 17:03:35

您所指的 -j 选项是由 make 而不是 gcc 处理的。

使用 make -j n 要求 make 通过多个并行进程运行 Makefile 中的操作(将 n 替换为对于 make -j 2 来说,它是 2 进程)。

在进行并行构建时,Make 将很好地处理大多数同步任务。

The -j option you are referring to is handled by make not gcc.

Using make -j n asks make to run the actions in the Makefile with multiple parallel process (Replace n with a number. In the case of make -j 2 it's 2 process).

Make will handle most synchronization tasks well when doing parallel builds.

鲸落 2024-10-26 17:03:35

用gold替换ld - 有经验吗?我已经进行了基准测试在最小综合基准上比较 LD vs gold vs LLVM LLD,时间结果清楚地表明 gold 和 LLVM LLD 都能够根据时间进行并行链接,例如:

nogold:  wall=4.35s user=3.45s system=0.88s 876820kB
gold:    wall=1.35s user=1.72s system=0.46s 739760kB
lld:     wall=0.73s user=1.20s system=0.24s 625208kB

我们知道使用了并行链接,因为在这两个 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.:

nogold:  wall=4.35s user=3.45s system=0.88s 876820kB
gold:    wall=1.35s user=1.72s system=0.46s 739760kB
lld:     wall=0.73s user=1.20s system=0.24s 625208kB

We know that parallel link was used because the wall time was smaller than the user time in both of those CPUs

Those benchmarks also reproduce Martin Richtarsky's 2x faster LLVM LLD link times.

念三年u 2024-10-26 17:03:35

以下是 CMake 上的两个选项(在 3.16.3 上测试):

find_program(GNU_GOLD_PROGRAM gold)
if (GNU_GOLD_PROGRAM)
    include(ProcessorCount)
    ProcessorCount(HOST_PROC_COUNT)
    add_link_options("-fuse-ld=gold;LINKER:--threads,--thread-count=${HOST_PROC_COUNT}")
endif(GNU_GOLD_PROGRAM)
  • 切换到 lld 链接器 (默认情况下在多线程模式下工作):
find_program(LLD_PROGRAM lld)
if(LLD_PROGRAM)
    add_link_options("-fuse-ld=lld")
endif(LLD_PROGRAM)

更完整的示例:此处

注意:如果您收到错误消息“ld. lld:错误:无法找到库...”您可能需要在 link_directories 调用中添加库目录列表,如下所示:

find_program(LLD_PROGRAM lld)
if(LLD_PROGRAM)
    add_link_options("-fuse-ld=lld")
    link_directories(/usr/lib64 /usr/local/lib64)
endif(LLD_PROGRAM)

Here are two options for those on CMake (tested on 3.16.3):

  • Switch to gold linker, enable multi-threading (set to number of system cores):
find_program(GNU_GOLD_PROGRAM gold)
if (GNU_GOLD_PROGRAM)
    include(ProcessorCount)
    ProcessorCount(HOST_PROC_COUNT)
    add_link_options("-fuse-ld=gold;LINKER:--threads,--thread-count=${HOST_PROC_COUNT}")
endif(GNU_GOLD_PROGRAM)
  • Switch to lld linker (works in multi-threading mode by default):
find_program(LLD_PROGRAM lld)
if(LLD_PROGRAM)
    add_link_options("-fuse-ld=lld")
endif(LLD_PROGRAM)

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:

find_program(LLD_PROGRAM lld)
if(LLD_PROGRAM)
    add_link_options("-fuse-ld=lld")
    link_directories(/usr/lib64 /usr/local/lib64)
endif(LLD_PROGRAM)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文