duanbi8089 2019-05-16 06:17 采纳率: 100%
浏览 632
已采纳

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

Compiling a Rust file with rustc foo.rs fails if I don't have a C++ toolchain installed. But when compiling a Go program, such a toolchain is not required. Why is that?

  • 写回答

1条回答 默认 最新

  • douguan1887 2019-05-16 10:56
    关注

    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:

    • Using lld rather than ld would allow shipping a single linker with rustc which can target all platforms.
    • Using cranelift as an alternative backend could also remove the dependency on ld.
    • The Redox project is working on relibc, a portable Rust implementation of the libc API.

    Those are all work in progress, and in the meantime rustc will require a C toolchain. Furthermore, even in the foreseeable future, I would expect rustc to require a C toolchain for targets not yet covered by any Rust toolchain, just so you can use the target without waiting for some hypothetical development.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵