Cargo test requires allocator in no_std/embedded crate

Hi there,
I've created a no_std crate for an embedded target, but when running cargo test it wan't compile due to a missing global allocator. However, I'm passing my custom allocator to the dev-dependencies but still no luck. I've also tried to use alloc-shim and alloc-no-stdlib without success.
So is there a way to include the standard allocator to successfully build with cargo test?

Thx in advance.

One option, which is admittedly kind of a blunt instrument, is to not use no_std when testing:

#![cfg_attr(not(test), no_std)]

Hey,
thanks for this hint. Unfortunately this only works for unit tests with cargo test --test. For Doc-Tests this is ignored and still raising this error, that the global allocator is missing :roll_eyes:

Any further hints?

Thanks in advance...

I believe the doctest cfg is now available on stable, so -- while this is admittedly getting uglier -- you could try

#![cfg_attr(not(any(test, doctest)), no_std)]

(cfg(test) used to be set for doctests, but it broke stuff, as I vaguely recall.)

The bigger problem you're running into is that tests and benches on no_std targets are not currently well supported...which kind of makes sense, as tests with the default framework need to be able to do things like print to stdio. I've successfully used the hack I'm suggesting here in one of my libraries.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.