在 Debian 上编译 Rust 程序或 Rust 本身,常见有两种场景。下面分别说明。
推荐用官方方式(比 apt 更新快):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示完成后:
source "$HOME/.cargo/env"
或重新登录终端。
验证:
rustc --version
cargo --version
也可用 apt(版本较旧):
sudo apt update sudo apt install rustc cargo
新建项目:
cargo new hello
cd hello
编译并运行:
cargo build
cargo run
发布版编译(优化):
cargo build --release
生成文件在:
target/debug/hello
target/release/hello
某些 crate 需要系统库:
sudo apt install -y build-essential pkg-config libssl-dev
例如:
openssl → libssl-dev-dev 包如果你想自己编译 rustc:
sudo apt install -y build-essential curl git python3 pkg-config libssl-dev
git clone https://github.com/rust-lang/rust.git
cd rust
./x.py build
编译结果在:
build/x86_64-unknown-linux-gnu/stage1/bin/rustc
⚠️ 非常耗时,一般不建议。
例如编译到 ARM:
rustup target add armv7-unknown-linux-gnueabihf
cargo build --target armv7-unknown-linux-gnueabihf
如果你能说明:
我可以给你更具体的步骤。