Search code examples
rustrust-cargorust-no-std

error[E0463]: can't find crate for `alloc` error from imported crate when building for thumbv8m.main-none-eabi


I am trying to use a library that uses ff crate in an embedded application, but as the title says, I am getting errors from the crate used in the imported library even if I enabled alloc feature flag inside of Cargo.toml.

I also double checked that I've added specific target which is thumbv8.main-none-eabi.

The following is a simple reproduction code:

file structure:

embd-app      <-- embed application, import some-crate
|--.cargo/config
|--Cargo.toml
|--src
    |--main.rs

some-crate    <-- a crate uses ff crate
|--Cargo.toml
|--src
    |--lib.rs

embd-app/.cargo/config:

[build]
target = "thumbv8m.main-none-eabi"

[unstable]
build-std = ["core"]

embd-app/Cargo.toml

[dependencies]
some-crate = { path = "../some-crate", default-features = false , features = ["alloc"] }

embd-app/src/main.rs

#![no_std]
#![no_main]
/// some code

some-crate/Cargo.toml

[package]
name = "some-crate"
version = "0.1.0"
edition = "2021"

[dependencies]
ff = { version="0.13.0", default-features = false }

[features]
default = ["std"]
alloc = ["ff/alloc"]
std = ["alloc"]

some-crate/src/lib.rs

// The code is minimized just to generate an error
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

use alloc::{vec, vec::Vec};

pub fn foo() -> Vec<u8> {
    vec![1, 2]
}

Target added:

$ rustup target list | grep installed
aarch64-apple-darwin (installed)
thumbv8m.main-none-eabi (installed)

compile error message:

error[E0463]: can't find crate for `alloc`
  --> /Users/foobar/.cargo/registry/src/github.com-1ecc6299db9ec823/ff-0.13.0/src/lib.rs:10:1
   |
10 | extern crate alloc;
   | ^^^^^^^^^^^^^^^^^^^ can't find crate

For more information about this error, try `rustc --explain E0463`.
error: could not compile `ff` due to previous error

I did not expect such an error message since I specified the features = ["alloc"] flag, is there something that is wrong?


Solution

  • As @jthulhu and @chaim-friedman mentioned at the comment, I could solve this problem by adding build-std = ["core", "alloc"].