In Rust, why does `cargo build --target aarch64-unknown-linux-gnu` fail with E0463 on a fresh laptop, and what fixes it?
answer
- the standard library ships prebuilt
- one copy per target triple
- only the host's copy by default
- a rustup subcommand, not a Cargo one
basics
~20 sEvery Rust build links against a precompiled standard library for its target triple, and rustup installs only the host's copy by default. Running rustup target add aarch64-unknown-linux-gnu installs the missing one; a linker for that platform is a separate step.
solid answer
~30 sThe error is E0463, ``can't find crate for `std` ``, and rustc's own note says the target may not be installed. A rustup toolchain ships the standard library crates prebuilt for each target triple as a `rust-std-<triple>` component, and a fresh install carries only the host's. `rustup target add aarch64-unknown-linux-gnu` adds that component to the active toolchain, and a `targets` list in `rust-toolchain.toml` gives every checkout the same set. After that Cargo writes the output under `target/aarch64-unknown-linux-gnu/debug/` instead of `target/debug/`. Installing the target brings no linker, though: linking an aarch64 Linux binary still needs a cross linker named in `.cargo/config.toml`.
code
plaintext · 7 lineserror[E0463]: can't find crate for `std`
|
= note: the `aarch64-unknown-linux-gnu` target may not be installed
= help: consider downloading the target with `rustup target add aarch64-unknown-linux-gnu`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `edge-agent` (bin "edge-agent") due to 1 previous errorgo deeper
Recall that the standard library comes prebuilt per target triple and that rustup target add installs a missing one. Quote the E0463 message and its rustup hint.
Explain what a target triple names, why rustup ships std as a rust-std component per triple, and where Cargo puts cross-compiled output. Note that core and alloc exist even where std does not.
Show the whole chain: std from rustup, a cross linker in Cargo config, a runner or a device to run it. Mention pinned toolchains needing the target too, and rust-toolchain.toml targets for repeatable machines.
Frame it as onboarding cost: which triples the repository declares so every laptop and CI image is set up the same way, and where the cross linker comes from.
## The error a fresh laptop prints A developer on an aarch64 macOS laptop, or on x86_64 Linux, needs a binary for an aarch64 Linux server and runs `cargo build --target aarch64-unknown-linux-gnu`. Cargo hands the triple to `rustc` as `--target`, and the first crate it compiles fails: ```plaintext error[E0463]: can't find crate for `std` | = note: the `aarch64-unknown-linux-gnu` target may not be installed = help: consider downloading the target with `rustup target add aarch64-unknown-linux-gnu` ``` **E0463** is rustc's general "can't find crate" error. The crate it cannot find here is not one of your dependencies: it is the **standard library** itself, which every crate links against unless it declares `#![no_std]`. ## Why the standard library exists once per target A **target triple** such as `aarch64-unknown-linux-gnu` names an architecture, a vendor, an operating system and an environment or ABI. A single `rustc` binary can generate code for every triple listed by `rustc --print target-list`; what it cannot do is produce that platform's compiled standard library on the spot. Those crates are compiled ahead of time and distributed per triple: - `core` and `alloc` are built even for bare-metal targets, which have no operating system. - `std` exists only where there is an operating system underneath, so the `thumbv7em-none-eabihf` set holds `core` and `alloc` but no `std`. - rustup packages each set as a component named `rust-std-<triple>`, and `rustup component list --installed` lists the ones you have. A default rustup install carries the `rust-std` component for the **host** triple and nothing else. Build for any other triple and rustc finds no compiled `std` to link against, which is exactly what E0463 reports. `rustup target list --installed` shows which triples the active toolchain can already build for. The rustc book's platform-support page sorts every triple into tiers; `aarch64-unknown-linux-gnu` is Tier 1 with host tools, so rustup distributes a prebuilt standard library for it and nothing has to be compiled from source. ## Fixing it with rustup 1. Run `rustup target add aarch64-unknown-linux-gnu`. It downloads that triple's `rust-std` component into the active toolchain; `--toolchain` picks a different one. 2. Build again with `cargo build --target aarch64-unknown-linux-gnu`. Compilation now gets as far as the link step. 3. Make it repeatable for the team: a `targets` list in `rust-toolchain.toml` tells rustup to install those triples for anyone who builds the repository. Pinning the toolchain itself belongs with the release-train and MSRV material. The component belongs to one toolchain. A project pinned to a specific release needs the target added to that release; adding it to `stable` does nothing for a build that runs on the pinned one. ## What changes once the target is installed - Output moves to `target/aarch64-unknown-linux-gnu/debug/` (or `.../release/`), so a cross build never overwrites the host build in `target/debug/`. - Build scripts and procedural macros still run on the build machine, so Cargo compiles them for the host and keeps them under `target/debug/`. - Typing `--target` every time is optional: `[build] target = "aarch64-unknown-linux-gnu"` in `.cargo/config.toml`, or the `CARGO_BUILD_TARGET` environment variable, sets a default that the `--target` flag still overrides. ## What `rustup target add` does not give you Installing the target fixes **compilation**, not **linking**. rustc infers the linker from the target, and on Unix-like targets that is a C compiler driver such as `cc`. The `cc` on a laptop is set up to link programs for the laptop, so the next build usually fails at the link step until `.cargo/config.toml` names a cross linker, for example `aarch64-linux-gnu-gcc` under `[target.aarch64-unknown-linux-gnu]`. Two more pieces are also outside rustup's reach: - C libraries that your dependencies link against must exist for the target, not for the host. - Running the result on the laptop needs an emulator configured as a `runner`; without one, `cargo run` executes the aarch64 Linux binary directly, and the laptop cannot run it. | Symptom | Missing piece | Fix | |---|---|---| | E0463, can't find crate for `std` | the target's precompiled standard library | `rustup target add <triple>` | | Compiles, then the link step fails | a linker for the target | `linker` under `[target.<triple>]` in `.cargo/config.toml` | | Builds, but `cargo run` cannot start it | a way to run foreign binaries | `runner` under the same table, or run it on the device | The order matters in an interview answer: first the standard library (rustup), then the linker (Cargo config), then a way to run what you built.
- Does `rustup target add` change which compiler or Cargo version builds the project?No. It adds a `rust-std` component to one toolchain, the active one unless `--toolchain` names another, and the same `rustc` and `cargo` binaries keep doing the work. Because components belong to a toolchain, a repository pinned to a specific release needs the target installed on that release; a `targets` list in `rust-toolchain.toml` makes rustup do this automatically.
- Why do build scripts still appear under `target/debug/` during a cross build?A build script and a procedural macro run on the build machine while Cargo compiles, so Cargo builds them for the host triple even when `--target` names another platform. Only the package and its normal dependencies are compiled for the target and placed under `target/aarch64-unknown-linux-gnu/`.
- Cargo downloads the target's standard library automatically the first time you build for it.
- Installing the target with rustup also installs a linker for that platform.
- E0463 here means a dependency is missing from the Cargo.toml dependency list.
- Each target platform needs its own separately installed rustc binary.
- Cross-compiled output lands in target/debug, replacing the host build.