Debian 下 Rust 编译出错的快速排查与修复
一、先按这套顺序定位
二、Debian 常见报错与对应修复
报错:linker ‘cc’ not found
原因:缺少 C 编译与链接工具链。
修复:安装基础编译工具 sudo apt install build-essential,再构建。若仍提示找不到链接器,可进一步安装 gcc 或 clang 并确保 PATH 正确。
报错:OpenSSL 相关(如 ld: cannot find -lssl 或 fatal error: openssl/ssl.h: No such file or directory)
原因:系统缺少 OpenSSL 开发库。
修复:安装 sudo apt install libssl-dev pkg-config cmake clang libclang-dev llvm,随后重新构建。
报错:target wasm32-unknown-unknown not found
原因:项目需要 wasm32 目标,但工具链未安装。
修复:安装 nightly 工具链并添加目标:
rustup update nightly
rustup target add wasm32-unknown-unknown --toolchain nightly
rustup update stable
验证:rustup show。
报错:can’t find crate for ‘core’(常见于 no_std/内核/裸机项目)
原因:未为目标编译 core 等标准库。
修复:在项目根目录创建自定义目标(如 x86_64-blog_os.json),并在 .cargo/config.toml 中启用 build-std:
[unstable]
build-std = [“core”, “compiler_builtins”]
build-std-features = [“compiler-builtins-mem”]
[build]
target = “x86_64-blog_os.json”
同时确保使用 nightly 工具链。
报错:编译被中断(signal: 9, SIGKILL)
原因:内存不足导致 OOM 被系统终止。
修复:增加交换空间(示例 4G):
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
或降低并行度/关闭默认特性:如 cargo build --release --no-default-features。
三、交叉编译要点(以 ARM 为例)
四、实用命令清单