Help enabling `no-std` feature in a crate

Hi :slight_smile:

I got a crate:

that depends of the num crate:

and i want enable a feature in my crate for compile in a no-std environment, for this in the Cargo.toml I have:

[dependencies]
num = { version = "0.4", default-features = false}

[features]
no-std = ["num/libm"]
default = ["num/std"]

also in lib.rs

#![cfg_attr(all(feature = "no-std"), no_std)]

this seems to work fine, but testing in a non-std environment I have an error, for example in the following test repo:

i have:

[dependencies.static-math]
path = "/home/elsuizo/Repos/static-math"
features = ["no-std"]

with the following error:

  --> /home/elsuizo/.cargo/registry/src/github.com-1ecc6299db9ec823/num-traits-0.2.14/src/lib.rs:21:1
   |
21 | extern crate std;
   | ^^^^^^^^^^^^^^^^^ can't find crate
   |
   = note: the `thumbv7em-none-eabihf` target may not be installed

looking in that lines of code in the num-traits repo:

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

It seems to not have the flag properly enabled, but I don't know more to try. Any help is welcome. Thanks in advance

It’s better to have an std feature that enables std, and make it a default feature.

1 Like

This leaves the default features of static-math enabled, which in turn enables std in num.

1 Like

yeah!!! the correct declaration was: :blush:

[dependencies.static-math]
default-features = false
path = "/home/elsuizo/Repos/static-math"
features = ["no-std"]

this is correctly specified in the documentation:

https://doc.rust-lang.org/cargo/reference/features.html

Note: This may not ensure the default features are disabled. If another dependency includes flate2 without specifying default-features = false, then the default features will be enabled. See feature unification below for more details.

Thanks!!!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.