Compilation error: can't find crate for `core`
Asked Answered
H

6

21

I'm using Rust 1.35.0 to try out some Rust examples and I could not get it to compile, as I keep getting the following message:

error[E0463]: can't find crate for `core`

I ran rustc --explain E0463 and I see the following message:

You need to link your code to the relevant crate in order to be able to use it
(through Cargo or the `-L` option of rustc example). Plugins are crates as
well, and you link to them the same way.

Here is my Cargo.toml:

[package]
name = "sensor-node"
version = "0.1.0"
authors = ["joesan <[email protected]>"]
edition = "2018"

[dependencies]
dwm1001 = "0.1.0"
panic-halt = "0.2.0"
nb = "0.1.1"

Here is my main.rs:

fn main() {
    let s = String::from("hello");  // s comes into scope

    takes_ownership(s);             // s's value moves into the function...
                                    // ... and so is no longer valid here

    let x = 5;                      // x comes into scope

    makes_copy(x);                  // x would move into the function,
                                    // but i32 is Copy, so it’s okay to still
                                    // use x afterward

} // Here, x goes out of scope, then s. But because s's value was moved, nothing
  // special happens.

fn takes_ownership(some_string: String) { // some_string comes into scope
    println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
  // memory is freed.

fn makes_copy(some_integer: i32) { // some_integer comes into scope
    println!("{}", some_integer);
} // Here, some_integer goes out of scope. Nothing special happens.
Hanan answered 31/5, 2019 at 6:2 Comment(7)
If you are cross compiling - os.phil-opp.com/cross-compile-libcoreBlooming
If you have a link to the examples you're following that might help as wellBlooming
It is just a simple program to test out Ownership concepts! I have added the main.rs to my post above! Nothing special in thatHanan
rustup target add x86_64-unknown-linux-gnu also did not help°Hanan
The example you are using is not for the PC, but for an embedded device. For running simple programs on the PC you should better start anew with something like cargo new my-example.Baum
It's hard to answer your question because it doesn't include a minimal reproducible example. We can't tell how you are attempting to compile the code. It would make it easier for us to help you if you try to reproduce your error on the Rust Playground if possible, otherwise in a brand new Cargo project, then edit your question to include the additional info. There are Rust-specific MCVE tips you can use to reduce your original code for posting here. Thanks!Dulles
I had the same error because my shell was defaulting to system-wide cargo installation in /usr/local/bin/cargo instead of one provided by rustup.Perpetual
B
4

Your code works fine on the Rust playground, so I recommend checking your Rust installation and environment settings.


You may want to use the preconfigured Rust Docker image to run your app. Have Docker installed, then:

docker pull rust

Go to your project folder and run:

docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust cargo run

Output:

hello
5

For your simple example on a PC, you don't need any of these dependencies:

[dependencies]
dwm1001 = "0.1.0"
panic-halt = "0.2.0"
nb = "0.1.1"

Here are my steps to test your sample on Linux:

cargo new hello
cd hello
code .

Open main.rs and paste your sample main.rs and save:

fn main() {
    let s = String::from("hello"); // s comes into scope

    takes_ownership(s); // s's value moves into the function...
                        // ... and so is no longer valid here

    let x = 5; // x comes into scope

    makes_copy(x); // x would move into the function,
                   // but i32 is Copy, so it’s okay to still
                   // use x afterward
} // Here, x goes out of scope, then s. But because s's value was moved, nothing
  // special happens.

fn takes_ownership(some_string: String) {
    // some_string comes into scope
    println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
  // memory is freed.

fn makes_copy(some_integer: i32) {
    // some_integer comes into scope
    println!("{}", some_integer);
} // Here, some_integer goes out of scope. Nothing special happens.

In a terminal inside the hello folder, run:

cargo run

And the output is good:

hello
5

This may help:

  1. Shell command

    rustup component list --installed
    

    Output:

    cargo-x86_64-unknown-linux-gnu
    clippy-x86_64-unknown-linux-gnu
    rls-x86_64-unknown-linux-gnu
    rust-analysis-x86_64-unknown-linux-gnu
    rust-docs-x86_64-unknown-linux-gnu
    rust-src
    rust-std-x86_64-unknown-linux-gnu
    rustc-x86_64-unknown-linux-gnu
    rustfmt-x86_64-unknown-linux-gnu
    
  2. Shell command:

    rustup show
    

    Output:

    Default host: x86_64-unknown-linux-gnu
    
    installed toolchains
    --------------------
    
    stable-x86_64-unknown-linux-gnu (default)
    nightly-x86_64-unknown-linux-gnu
    
    active toolchain
    ----------------
    
    stable-x86_64-unknown-linux-gnu (default)
    rustc 1.35.0 (3c235d560 2019-05-20)
    
Basutoland answered 31/5, 2019 at 6:54 Comment(13)
The reason why I have those dependencies is to do something else, but since I'm starting out with Rust, I wanted to test ownership concepts first! So I do need those dependencies!Hanan
OK, with these extra dependencies present, the cargo run still runs OK, but takes more time.Basutoland
May be clean install of rust may solve the problem. (It seems)Basutoland
rustup self uninstall - Will try this and then do a fresh install to see if it works!Hanan
Your code also works fine (press Run) on the rust playground.Basutoland
How can I change the default host for this specific project? Right now, my default host is: x86_64-apple-darwin which I guess is why I get this errorHanan
Use rustup target help to get help. and use rustup target list to get list of installed and available targets, use rustup toolchain help and rustup toolchain list. see here.Basutoland
I still have this issue on LinuxDionysus
@chovy: check this and thisBasutoland
2nd link i already tried. I get this error: ` = note: /usr/bin/ld:/tmp/rustcPJoEQl/list:6: syntax error in VERSION script collect2: error: ld returned 1 exit status`Dionysus
@Dionysus see this. Is your code working in the Rust playground?Basutoland
I don’t really know anything about rust. Just trying to get a dfinity canister working.Dionysus
@chovy: How about this?Basutoland
W
8

I solved this in my case with:

rustup target add wasm32-unknown-unknown
Welles answered 9/8, 2023 at 0:4 Comment(0)
J
5

In the context of MacOS, Homebrew and WASM.

It didn't work to run rustup target add wasm32-unknown-unknown. It kept failing until I decided to uninstall anything related to Rust, and decided to follow the official website method only:

  1. Uninstall Rust by running rustup self uninstall.
  2. Uninstall Rust Homebrew by running brew uninstall rust
  3. Reinstall Rust by following the official website suggestion by running curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh (please consult official Rust website in case it changed)
  4. Add the target: rustup target add wasm32-unknown-unknown
  5. Build using target: cargo build --target wasm32-wasi or cargo build --target wasm32-unknown-unknown
Jacey answered 20/1, 2024 at 22:19 Comment(1)
For me, the steps 2, 4, and a modified step 5 (--target wasm32-unknown-unknown) did the trick. Step 2 I wanted to do long ago already :-)Archiearchiepiscopacy
B
4

Your code works fine on the Rust playground, so I recommend checking your Rust installation and environment settings.


You may want to use the preconfigured Rust Docker image to run your app. Have Docker installed, then:

docker pull rust

Go to your project folder and run:

docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust cargo run

Output:

hello
5

For your simple example on a PC, you don't need any of these dependencies:

[dependencies]
dwm1001 = "0.1.0"
panic-halt = "0.2.0"
nb = "0.1.1"

Here are my steps to test your sample on Linux:

cargo new hello
cd hello
code .

Open main.rs and paste your sample main.rs and save:

fn main() {
    let s = String::from("hello"); // s comes into scope

    takes_ownership(s); // s's value moves into the function...
                        // ... and so is no longer valid here

    let x = 5; // x comes into scope

    makes_copy(x); // x would move into the function,
                   // but i32 is Copy, so it’s okay to still
                   // use x afterward
} // Here, x goes out of scope, then s. But because s's value was moved, nothing
  // special happens.

fn takes_ownership(some_string: String) {
    // some_string comes into scope
    println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
  // memory is freed.

fn makes_copy(some_integer: i32) {
    // some_integer comes into scope
    println!("{}", some_integer);
} // Here, some_integer goes out of scope. Nothing special happens.

In a terminal inside the hello folder, run:

cargo run

And the output is good:

hello
5

This may help:

  1. Shell command

    rustup component list --installed
    

    Output:

    cargo-x86_64-unknown-linux-gnu
    clippy-x86_64-unknown-linux-gnu
    rls-x86_64-unknown-linux-gnu
    rust-analysis-x86_64-unknown-linux-gnu
    rust-docs-x86_64-unknown-linux-gnu
    rust-src
    rust-std-x86_64-unknown-linux-gnu
    rustc-x86_64-unknown-linux-gnu
    rustfmt-x86_64-unknown-linux-gnu
    
  2. Shell command:

    rustup show
    

    Output:

    Default host: x86_64-unknown-linux-gnu
    
    installed toolchains
    --------------------
    
    stable-x86_64-unknown-linux-gnu (default)
    nightly-x86_64-unknown-linux-gnu
    
    active toolchain
    ----------------
    
    stable-x86_64-unknown-linux-gnu (default)
    rustc 1.35.0 (3c235d560 2019-05-20)
    
Basutoland answered 31/5, 2019 at 6:54 Comment(13)
The reason why I have those dependencies is to do something else, but since I'm starting out with Rust, I wanted to test ownership concepts first! So I do need those dependencies!Hanan
OK, with these extra dependencies present, the cargo run still runs OK, but takes more time.Basutoland
May be clean install of rust may solve the problem. (It seems)Basutoland
rustup self uninstall - Will try this and then do a fresh install to see if it works!Hanan
Your code also works fine (press Run) on the rust playground.Basutoland
How can I change the default host for this specific project? Right now, my default host is: x86_64-apple-darwin which I guess is why I get this errorHanan
Use rustup target help to get help. and use rustup target list to get list of installed and available targets, use rustup toolchain help and rustup toolchain list. see here.Basutoland
I still have this issue on LinuxDionysus
@chovy: check this and thisBasutoland
2nd link i already tried. I get this error: ` = note: /usr/bin/ld:/tmp/rustcPJoEQl/list:6: syntax error in VERSION script collect2: error: ld returned 1 exit status`Dionysus
@Dionysus see this. Is your code working in the Rust playground?Basutoland
I don’t really know anything about rust. Just trying to get a dfinity canister working.Dionysus
@chovy: How about this?Basutoland
T
1

When you first install a toolchain, rustup installs only the standard library for your host platform - that is, the architecture and operating system you are presently running. To compile to other platforms you must install other target platforms. This is done with the rustup target add command. For example, to add the Android target:

See The Rustup Book - Cross-compilation

For example:

$ rustup target add aarch64-unknown-linux-gnu
info: downloading component 'rust-std' for 'aarch64-unknown-linux-gnu'
info: installing component 'rust-std' for 'aarch64-unknown-linux-gnu'

To get the target string on the target machine, if rust is installed, you can run:

rustc -vV

e.g. output

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: aarch64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.6

The host field can be used as the argument to rustup target add

Note: You may run into linker issues, in which case you can configure your linker, see Cargo Book - Configuration - Target

Trivium answered 8/3, 2023 at 14:3 Comment(0)
R
0

This error can occur when omitting the edition parameter. The core crate is not available in old editions of Rust. Although you have that parameter in the Cargo.toml file, if you try to compile directly using the rustc commandline, that parameter must be supplied manually. As in

rustc --edition 2018 <filename>
Radioactivate answered 14/10, 2023 at 15:56 Comment(0)
S
0

If the wasm32-unknown-unknown target is up-to-date, try checking the PATH variable of your shell. I had the same issue until I modified the PATH by adding the path to the .cargo/bin directory.

PATH=/Users/$USER/.cargo/bin:$PATH
Schober answered 2/7, 2024 at 21:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.