温馨提示×

rust在debian如何编译

小樊
33
2026-10-08 02:35:24
栏目: 编程语言

在 Debian 上编译 Rust 程序或 Rust 本身,常见有两种场景。下面分别说明。


一、在 Debian 上编译 Rust 程序(最常见)

1. 安装 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

2. 编译 Rust 项目

新建项目:

cargo new hello
cd hello

编译并运行:

cargo build
cargo run

发布版编译(优化):

cargo build --release

生成文件在:

target/debug/hello
target/release/hello

3. 常见依赖(如需)

某些 crate 需要系统库:

sudo apt install -y build-essential pkg-config libssl-dev

例如:

  • openssl → libssl-dev
  • 图形/音频 → 对应 -dev 包

二、在 Debian 上从源码编译 Rust 编译器(不常用)

如果你想自己编译 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

如果你能说明:

  • 是 编译 Rust 程序 还是 编译 Rust 编译器
  • Debian 版本(如 11 / 12)
  • 是否交叉编译或缺少某个错误

我可以给你更具体的步骤。

0 踩