Rust Bare-Metal Cross-Compilation for AArch64: can't find crate for `core` - rust

I am trying to cross-compile an example rust code as library for bare-metal AArch64 on Linux (KDE-Neon). Unfortunately it does not work. This is my example rust code (lib.rs):
#![no_std]
#[no_mangle]
pub extern "C" fn double_value (a : u32) -> u32
{
a / 2
}
According to [1] I first installed rustup with:
sudo snap install rustup --classic
Afterwards, I followed [2] and ran:
rustup toolchain list
rustup install stable
rustup default stable
Then I followed [1] and [3] and ran:
rustup target add aarch64-unknown-none
However when I try to compile afterwards, I doesn't work, neither with rustc nor with cargo:
rustc:
rustc --crate-type=lib lib.rs --target=aarch64-unknown-none
error[E0463]: can't find crate for `core`
|
= note: the `aarch64-unknown-none` target may not be installed
error: aborting due to previous error
cargo:
Cargo.toml:
[package]
name = "rust_baremetal_lib"
version = "0.1.0"
edition = "2018"
[lib]
name = "rust_baremetal_lib"
path = "src/lib.rs"
crate-type = ["staticlib"]
[dependencies]
cargo build --lib --target=aarch64-unknown-none
Compiling rust_baremetal_lib v0.1.0 (/home/kilian/code/rust_link/rust_baremetal_lib)
error[E0463]: can't find crate for `core`
|
= note: the `aarch64-unknown-none` target may not be installed
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
error: could not compile `rust_baremetal_lib`
To learn more, run the command again with --verbose.
To me it looks like rustc and cargo cannot find the core library, although it should be installed, as seen when running rustc --print:
rustc --print target-list|grep arch64-unknown-none
aarch64-unknown-none
aarch64-unknown-none-softfloat
I already looked on the internet but didn't find any clues unfortunately. I hope someone can help me find the issue!
[1] https://rust-lang.github.io/rustup/cross-compilation.html
[2] No default toolchain configured after installing rustup
[3] https://doc.rust-lang.org/nightly/rustc/platform-support.html

The problem seemed to result from a broken rust installation. I removed all packages related to rust that I could found via apt and snap. Afterwards I reinstalled rust via the recommended way [1]. Then I again ran:
rustup target add aarch64-unknown-none
Afterwards Cargo complained, that a "panic handler" was missing in my example code, so I inserted one, following [2]. My example code now looks like this:
#![no_std]
use core::{
panic::PanicInfo,
};
#[no_mangle]
pub extern "C" fn double_value (a : u32) -> u32
{
a * 2
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {
continue;
}
}
Finally I can now cross-compile this example to an AArch64 bare metal static library with:
cargo build --lib --target=aarch64-unknown-none
[1] https://www.rust-lang.org/tools/install
[2] https://interrupt.memfault.com/blog/zero-to-main-rust-1

Related

Unable to use rust crates

I'm new to rust. I'm following a getting started tutorial that imports the crate random-number but when running the code I'm getting the error can't find crate for 'random_number'. What am I doing wrong?
~/Cargo.toml:
[package]
name = "test"
version = "0.0.1"
edition = "2021"
[dependencies]
random-number = "0.1.8"
~/src/main.rs:
extern crate random_number;
use random_number::random;
fn main() {
let num: i8 = random!(..);
println!("{}", num);
}
rustc is not meant to be used directly. It is the compiler that can compile a .rs file, but it doesn't have any dependency manager attached to it. So if you decide to use rustc directly, you need to manage your dependencies manually.
cargo is the official tool to compile Rust projects. It internally uses rustc, but additionally manages the project's dependencies that are specified in Cargo.toml.
cargo build --release && ./target/release/<project_name>
or the short form:
cargo run --release

Could not compile `lazy_static`

I'm having a hard time cross-compiling an embedded Rust project that worked before for a raspberry pi. I have all the needed deps in Cargo.toml but on doing:
$ cargo build --target thumbv7m-none-eabi
I get the following error.
error[E0463]: can't find crate for `std`
--> /home/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-1.4.0/src/inline_lazy.rs:9:1
|
9 | extern crate std;
| ^^^^^^^^^^^^^^^^^ can't find crate
|
= note: the `thumbv7m-none-eabi` target may not support the standard library
= help: consider building the standard library from source with `cargo build -Zbuild-std`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `lazy_static` due to previous error
$ rustup show
installed targets for active toolchain
--------------------------------------
thumbv7m-none-eabi
x86_64-unknown-linux-gnu
active toolchain
----------------
nightly-x86_64-unknown-linux-gnu (default)
rustc 1.64.0-nightly (38b72154d 2022-07-11)
Compilation used to work previously without lazy_static as a dependency in cargo.toml,now I don't understand why this is happening.
By default lazy_static depends on the rust standard library, which as the compiler told you
may not be supported on the thumbv7m-none-eabi target
If you do not need the standard library in your project you can enable the no-std feature of lazy_static like this:
lazy_static = { version = "1.5.0", features = ["spin_no_std"] }
as described here.

What is the Rust/Cargo equivalent of `mvn install` for a personal library not uploaded to crates.io? [duplicate]

I've made a library:
cargo new my_lib
and I want to use that library in a different program:
cargo new my_program --bin
extern crate my_lib;
fn main {
println!("Hello, World!");
}
what do I need to do to get this to work?
They aren't in the same project folder.
.
├── my_lib
└── my_program
Hopefully this makes sense.
I thought I'd be able to override the path as per the Cargo guide, but it states
You cannot use this feature to tell Cargo how to find local unpublished crates.
This is when using the latest stable version of Rust (1.3).
Add a dependency section to your executable's Cargo.toml and specify the path:
[dependencies.my_lib]
path = "../my_lib"
or the equivalent alternate TOML:
[dependencies]
my_lib = { path = "../my_lib" }
Check out the Cargo docs for specifying dependencies for more detail, like how to use a git repository instead of a local path.
I was looking for an equivalent to mvn install. While this question is not quite a duplicate of my original question, anyone who stumbles across my original question and follows the link here will find a more complete answer.
The answer is "there is no equivalent to mvn install because you have to hard-code the path in the Cargo.toml file which will probably be wrong on someone else's computer, but you can get pretty close."
The existing answer is a bit brief and I had to flail around for a bit longer to actually get things working, so here's more detail:
/usr/bin/cargo run --color=always --package re5 --bin re5
Compiling re5 v0.1.0 (file:///home/thoth/art/2019/radial-embroidery/re5)
error[E0432]: unresolved import `embroidery_stitcher`
--> re5/src/main.rs:5:5
|
5 | use embroidery_stitcher;
| ^^^^^^^^^^^^^^^^^^^ no `embroidery_stitcher` in the root
rustc --explain E0432 includes this paragraph that echos Shepmaster's answer:
Or, if you tried to use a module from an external crate, you may have missed
the extern crate declaration (which is usually placed in the crate root):
extern crate core; // Required to use the `core` crate
use core::any;
Switching from use to extern crate got me this:
/usr/bin/cargo run --color=always --package re5 --bin re5
Compiling embroidery_stitcher v0.1.0 (file:///home/thoth/art/2019/radial-embroidery/embroidery_stitcher)
warning: function is never used: `svg_header`
--> embroidery_stitcher/src/lib.rs:2:1
|
2 | fn svg_header(w: i32, h: i32) -> String
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(dead_code)] on by default
Compiling re5 v0.1.0 (file:///home/thoth/art/2019/radial-embroidery/re5)
error[E0603]: function `svg_header` is private
--> re5/src/main.rs:8:19
|
8 | let mut svg = embroidery_stitcher::svg_header(100,100);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I had to slap a pub on the front of that function
pub fn svg_header(w: i32, h: i32) -> String
Now it works.

cannot find function `get_platform` in sdl2 0.31.0

I'm using the latest version of sdl2 (0.31.0) but cannot access get_platform:
extern crate sdl2;
pub fn main() {
println!("{}", sdl2::get_platform());
}
$ cargo run
Compiling repro v0.1.0 (file:///private/tmp/repro)
error[E0425]: cannot find function `get_platform` in module `sdl2`
--> src/main.rs:4:30
|
4 | println!("{}", sdl2::get_platform());
| ^^^^^^^^^^^^ not found in `sdl2`
I tried with use sdl2::*; and with cargo +nightly run, but neither removed the error.
The documentation you are reading is is not for the version you are using. Build it yourself (cargo doc --open) or view it on docs.rs.
That function was added recently and has not been released yet. Perhaps you should file an issue for the authors of the crate to let them know that having documentation that doesn't correspond to any released code is confusing.
In the meantime, you can use a git dependency if you really need it.

error: can't find crate

I'm trying to use this library.
But, cargo build says this:
Compiling test v0.1.0 (file:///C:/path/to/project/test)
src\main.rs:1:1: 1:28 error: can't find crate for `jvm_assembler` [E0463]
src\main.rs:1 extern crate jvm_assembler;
^~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `test`.
To learn more, run the command again with --verbose.
My Cargo.toml is this:
[package]
name = "test"
version = "0.1.0"
authors = ["yomizu_rai"]
[dependencies]
jvm-assembler = "*"
src/main.rs is this, and there are no other sourcefiles.
extern crate jvm_assembler;
use jvm_assembler::*;
fn main() {}
I think my Cargo.toml is not wrong, and src/main.rs has no room for mistake.
Why can not rustc find jvm-assembler?
How do I resolve?
Cargo can only find crates by name if they are on crates.io. In your case you need to specify the git URL, see the section on dependencies in the Cargo documentation.

Resources