为什么Rust要求使用C ++工具链来生成Rust二进制文件,而Go之类的语言却没有此要求?

Jac*_*123 3 c++ build-tools go toolchain rust

rustc foo.rs如果我没有安装C ++工具链,则使用编译Rust文件将失败。但是在编译Go程序时,不需要这样的工具链。这是为什么?

Mat*_* M. 12

TL;DR: Because everyone has a C toolchain.

Slight correction: rustc does not require a C++ toolchain, it only requires a C toolchain. Notably, rustc created binaries only depend on libc (or equivalent), not on libstdc++ (or equivalent).


As Go demonstrated, it is possible to not require a C toolchain. You only need to re-implement its functionality:

  • You need to implement your own linker, respecting the target platform format.
  • You need to implement your own libc (aka OS layer).

There are advantages to doing so, such as possibly faster compilation or easier cross-compiling, however there is a cost in doing the implementation, and it's easy to get things wrong.


The Rust community preferred to put more effort into the language than in the toolchain, and therefore reusing the stock toolchain was easier. Specifically, rustc will require a platform linker (ld on Unix) and platform equivalent to libc.

This is not a core design principle, it's just a pragmatic approach, and there are projects to cut down on these dependencies:

  • 使用lld而不是ld将允许使用rustc交付单个链接程序,该链接程序可以定位所有平台。
  • 使用Cranelift作为替代后端也可以消除对ld的依赖。
  • Redox项目正在研究relibclibcAPI 的可移植Rust实现)。

这些都在进行中,与此同时,rustc需要C工具链。此外,即使在可预见的将来,我也希望rustc对于尚未被任何Rust工具链覆盖的目标要求使用C工具链,这样您就可以使用目标而无需等待任何假想的开发。

  • 也许 OP 正在谈论 C++ 工具链,因为他使用 Windows 操作系统。看起来C 和C++ 工具链之间没有明显的区别。 (2认同)