Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Rust: linker cc not found when compiling

Rust compiles to object files and hands them to a linker. If cc is missing, cargo fails at the last step: install a C toolchain or pick another linker.

~5 min readRust
#rust#cargo#linking#build-tools
Bold white headline reading LINKER MISSING with a Rust crate passing cyan object files to an empty linker slot.

Introduction & Quick Summary

Compilation gets through every crate and fails at the very last step:

code
error: linker `cc` not found | = note: No such file or directory (os error 2) error: could not compile `myapp` (bin "myapp") due to 1 previous error
code
error: linking with `cc` failed: exit status: 127 = note: collect2: fatal error: cannot find 'ld'

cc is the system C compiler, and rustc uses it as the linker for the x86_64-unknown-linux-gnu target. rustc compiles Rust to object files itself, then hands them to the platform linker together with the C runtime and system libraries. When no cc exists on the path, the build fails after all the Rust code has already been compiled successfully.

Where it appears:

  • A slim or Alpine container with only rustup installed and no build tools.
  • A CI image that installs Rust but not build-essential, gcc, or musl-dev.
  • A cross-compilation target whose linker is not installed or not named in config.
  • A machine where cc exists for one user but not in the service's PATH.
  • A build script that needs a C compiler for a -sys crate, failing earlier but with similar wording.

Quick answer: install a C toolchain for the target, or point rustc at a linker you already have such as rust-lld. Then confirm the build script crates compile too.

Core Mechanics, Signatures & Reproducible Setup

rustc resolves a linker in this order: -C linker on the command line, target.<triple>.linker in .cargo/config.toml, the CARGO_TARGET_<TRIPLE>_LINKER environment variable, then the target default, which is cc for the GNU Linux targets. It invokes that program with object files and library flags.

# a container that reproduces the failure docker run --rm -it rust:1.90-slim bash -lc ' rustc --version; command -v cc || echo "no cc"; cargo new demo && cd demo && cargo build; '
code
rustc 1.90.0 no cc Compiling demo v0.1.0 (/demo) error: linker `cc` not found

The same crate with the toolchain installed:

dockerfile
FROM rust:1.90-slim RUN apt-get update \ && apt-get install -y --no-install-recommends build-essential pkg-config \ && rm -rf /var/lib/apt/lists/* WORKDIR /app COPY . . RUN cargo build --release

Linking-adjacent errors and what each actually means:

ErrorMeaningFix direction
linker 'cc' not foundthe program is not on PATHinstall or configure a linker
exit status: 127the linker command did not runsame, often a missing ld
undefined reference to 'X'linker ran, a symbol is missingadd the library or a -sys crate
cannot find -lssllinker ran, library path is missinginstall the dev package
error: linking with 'cc' failedgeneric wrapper once the linker ranread the note under it
note: collect2: fatal errorgcc found but binutils missinginstall binutils

Key Comparison Table

TargetDefault linkerPackage that provides it
x86_64-unknown-linux-gnucc (gcc or clang)build-essential on Debian, gcc on Fedora
aarch64-unknown-linux-gnuaarch64-linux-gnu-gccgcc-aarch64-linux-gnu
x86_64-unknown-linux-muslmusl-gcc or rust-lldmusl-tools
x86_64-pc-windows-msvclink.exeVisual Studio Build Tools with the C++ workload
x86_64-apple-darwincc (clang)Xcode command line tools
wasm32-unknown-unknownrust-lldnothing extra

Linker choices for a Linux build host:

LinkerInstallTrade-off
GNU ld via ccbuild-essentialmost compatible, slowest link
clang plus lldclang lldfaster links, needs matching runtime flags
rust-lldships with the toolchainno extra package, good for CI and cross builds
moldseparate packagefastest for large binaries, extra dependency

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

gcc alone is not always enough: the C runtime headers and binutils are separate packages on minimal images.

bash
# Debian and Ubuntu apt-get update && apt-get install -y --no-install-recommends build-essential # Alpine apk add --no-cache build-base # Fedora and RHEL dnf groupinstall -y "Development Tools"
bash
command -v cc gcc ld cc --version | head -1

If cc reports a version but linking still fails with collect2, install binutils explicitly; some slim images include the compiler without the linker driver.

2. Use rust-lld when you cannot install a toolchain

rust-lld ships with the Rust toolchain and needs no system package. Point the target at it in the project's cargo config.

toml
# .cargo/config.toml [target.x86_64-unknown-linux-gnu] linker = "rust-lld"
bash
cargo build --release

This works because rust-lld understands the ELF object format rustc produced. It does not remove the need for a C library at run time, so a -sys crate that links libssl still needs libssl-dev. Rust 1.90 changed the default linker for some targets to rust-lld, which is why a project pinned to an older toolchain and a project on 1.90 can behave differently on the same machine.

3. Configure a linker per target

A cross target needs its own linker, and the setting belongs in config rather than in a shell alias so CI and local builds agree.

toml
# .cargo/config.toml [target.aarch64-unknown-linux-gnu] linker = "aarch64-linux-gnu-gcc" [target.x86_64-unknown-linux-musl] linker = "x86_64-linux-musl-gcc"
bash
rustup target add aarch64-unknown-linux-gnu apt-get install -y gcc-aarch64-linux-gnu cargo build --release --target aarch64-unknown-linux-gnu
bash
# the environment variable wins over config when you need to override once CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=clang cargo build --target aarch64-unknown-linux-gnu

Check the chosen linker with cargo build -v; the first line of the rustc invocation shows -C linker=....

4. Build static binaries with musl

A statically linked binary removes the runtime C library dependency, but the musl target still needs a linker and static libraries.

bash
apt-get install -y musl-tools musl-dev rustup target add x86_64-unknown-linux-musl cargo build --release --target x86_64-unknown-linux-musl file target/x86_64-unknown-linux-musl/release/myapp # ELF 64-bit LSB pie executable, statically linked

A -sys crate that compiles C code needs a musl-targeted C compiler; the usual fix is CC_x86_64_unknown_linux_musl=musl-gcc, checked with the environment tool before the build to confirm the value actually reaches cargo.

A cc-based crate compiled by cc-rs fails much earlier than the final link, and the message names the crate rather than the binary.

code
error: failed to run custom build command for `openssl-sys v0.9.104` --- stderr thread 'main' panicked at build/main.rs:120: Could not find directory of OpenSSL installation
bash
apt-get install -y pkg-config libssl-dev cargo clean -p openssl-sys cargo build

pkg-config is the piece most often missing: the headers exist, but the build script cannot discover them. If the message mentions cc itself, the compiler is missing; if it mentions a library path, the development package is.

Common Compilation/Runtime Mistakes & Version Gotchas

Reading the second error line as the cause. linking with 'cc' failed is emitted after the linker ran and returned non-zero. The note under it names the real problem, such as a missing library or an undefined symbol.

Setting linker with a path and no quotes in config. TOML accepts a path without quotes only when it has no spaces; a Windows path with backslashes needs either forward slashes or escaped backslashes. A malformed config is ignored with a warning, and the build falls back to cc.

Installing the compiler but not the runtime. gcc without libc6-dev produces a compiler that cannot find Scrt1.o. Install the meta package (build-essential, build-base) rather than individual tools.

Assuming rust-analyzer or cargo check needs the linker. cargo check does not link, so a project can pass check and fail build. Run cargo build in CI, not only check, or the missing linker is discovered at release time.

Overriding CC for the wrong crate. CC affects C code compiled by cc-rs; the Rust link step uses the target linker. A build that needs both needs both settings, and mixing them produces confusing partial fixes.

Version differences worth knowing. Rust 1.90 moved several Linux targets to rust-lld by default, so images that relied on cc may now build without it while older toolchains still fail. -Z flags for alternate linkers are nightly-only; on stable, use the -C linker form or a config entry. Cross builds for musl changed their recommended flags across releases, so pin the toolchain in rust-toolchain.toml when a build must be reproducible.

Verifying the result. After installing tools, run cargo build --release from a clean target directory and compare the dependency tree with Diff if a lockfile changed. Copy the released binary into a plain scratch or alpine image and start it: a binary that needs a shared library at runtime fails there even though the build succeeded.

Reference material: Cargo configuration and rustc command-line arguments.

Official references

primary documentation

The Rust documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Rust work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Rust guides

More in Rust

All Rust guides

The guides filed next to this one in Rust, in library order.