在 CentOS 上编译 Rust 程序一般有以下几个步骤。下面以 CentOS 7 / 8 / Stream 为例说明。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选择安装方式(一般直接回车默认即可)。
安装完成后加载环境:
source $HOME/.cargo/env
验证:
rustc --version
cargo --version
# CentOS 7
yum install rust cargo -y
# CentOS 8 / Stream
dnf install rust cargo -y
⚠️ 仓库中的 Rust 版本通常较老,不建议用于新项目。
cargo new hello
cd hello
cargo build
生成文件在:
target/debug/hello
cargo build --release
生成文件在:
target/release/hello
cargo run
Rust 某些依赖需要 C 编译器和开发库:
# CentOS 7
yum groupinstall "Development Tools" -y
# CentOS 8 / Stream
dnf groupinstall "Development Tools" -y
或:
yum install gcc gcc-c++ make -y
yum install openssl-devel -y
如果是新版 OpenSSL(如 openssl-sys):
export OPENSSL_DIR=/usr
CentOS 7 默认 glibc 较旧,编译出的程序在其他系统可能不兼容,但本机运行没问题。
如需跨发行版,可:
musl 静态编译rustup target add x86_64-unknown-linux-musl
cargo build --release --target x86_64-unknown-linux-musl
cargo new demo
cd demo
cargo run
输出:
Hello, world!
如果你有 具体的 CentOS 版本 或 编译报错信息,可以贴出来,我可以帮你具体排查。