Fixed cargo build with a custom target errors while learning blog_os

tianlang 发布于2025年06月17日14:23

Building a Rust OS: Issue and Solution

Problem

While following the Minimal Rust Kernel tutorial from the blog_os project, I encountered multiple errors when running cargo build with a custom target (x86_64-fusion_os.json):

$ cargo build --target x86_64-fusion_os.json
   Compiling std v0.0.0
error[E0432]: unresolved imports `super::key::Key`, `super::key::LazyKey`, `super::key::get`, `super::key::set`
...
error[E0425]: cannot find function `fill_bytes` in module `sys`
...
error[E0277]: the trait bound `System: core::alloc::GlobalAlloc` is not satisfied
...
error[E0599]: no method named `alloc` found for struct `System` in the current scope
...
error: could not compile `std` (lib) due to 15 previous errors

These errors indicate issues with missing imports, functions, and trait implementations, primarily related to the standard library and memory allocation.

Solution

To resolve these errors, enable the alloc crate by adding the following configuration to the .cargo/config.toml file:

[unstable]
build-std = ["core", "alloc", "compiler_builtins"]

Then, rebuild it:

cargo build --target x86_64-fusion_os.json
   Compiling fusion_os v0.1.0
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.31s

This configuration ensures the required core, alloc, and compiler_builtins crates are included, allowing the custom target to compile successfully.