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?

为什么Rust要求使用C ++工具链来生成Rust二进制文件,而Go之类的语言却没有此要求?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
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 onlibstdc++
(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 tolibc
.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 Qt 不小心删除了自带的类,该怎么办
- ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
- ¥15 有没有哪位厉害的人可以用C#可视化呀
- ¥15 可以帮我看看代码哪里错了吗
- ¥15 设计一个成绩管理系统
- ¥15 PCL注册的选点等函数如何取消注册
- ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
- ¥15 Python爬虫程序
- ¥15 crypto 这种的应该怎么找flag?
- ¥15 代码已写好,求帮我指出错误,有偿!