启用bincode default-features=false后no_std环境编译失败求助
问题描述
我尝试在no_std环境中使用bincode crate,已在Cargo.toml中为bincode设置default-features = false,但编译仍失败。我的依赖配置如下:
[package] name = "fsys" version = "0.1.0" edition = "2021" [dependencies] bincode = { version = "1.3.3", default-features = false}
我对此感到困惑:bincode的std特性已禁用,但它的依赖仍尝试使用标准库。以下是简化后的错误信息:
Compiling serde v1.0.182 error[E0463]: can't find crate for `std` | = note: the `x86_64-cos` target may not support the standard library = note: `std` is required by `serde` because it does not declare `#![no_std]` = help: consider building the standard library from source with `cargo build -Zbuild-std`
原始错误信息包含2万多行std导入失败的内容。
解决方案
问题出在bincode的依赖serde上:默认情况下serde会启用std特性,即便你禁用了bincode的默认特性,serde的std特性依然会被开启,导致它依赖标准库。
你需要在Cargo.toml中显式禁用serde的默认特性,同时启用serde的alloc特性(bincode在no_std环境下需要这个特性来分配内存):
[package] name = "fsys" version = "0.1.0" edition = "2021" [dependencies] bincode = { version = "1.3.3", default-features = false } serde = { version = "1.0.182", default-features = false, features = ["alloc"] }
配置完成后,serde会以no_std模式编译,仅依赖alloc crate而非标准库,与bincode的no_std环境兼容。
另外需确认项目根文件已添加#,或#![no_std]搭配extern crate alloc;(若使用alloc特性)。
内容的提问来源于stack exchange,提问作者kamkow1
相关产品推荐
相关产品推荐

