I am currently working on an embedded development project using the Rust.
However, when I tried to use my own library, which I had been using individually (and as a standard library), in an application on the embedded side, an error occurred as shown in the title.
error: no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait
For the sake of brevity and sharing, we will consider a reproducible folder structure as follows.
- foo
|__ app
| |__ src
| | |__ main.rs
| |__ Cargo.toml
|
|__ some-crate
|__ src
|__ lib.rs
main.rs is currently look like below:
#![no_std]
#![no_main]
#![feature(default_alloc_error_handler)]
use sample_crate::add;
#[no_mangle]
extern "C" fn sample_main() {
loop {
add(2, 2);
}
}
and some-crate/lib.rs
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(unused_extern_crates, dead_code)]
#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
#[macro_use]
extern crate alloc;
pub fn add(left: usize, right: usize) -> usize {
left + right
}
It could compile if I do not import some-crate::add
.
How can I make this compile with third party crate?