Self-hosted service
rust-cross/cargo-zigbuild avatar
rust-cross/cargo-zigbuild

cargo-zigbuild makes zig the linker, and -nostdinc is the bill

Compile Cargo project with zig as linker

2,723 stars110 forksRustMIT

At a glance

What is it?
A cargo subcommand that swaps zig in as the linker so cross compilation needs fewer system toolchains. The glibc suffix you can put on a target is a request rather than a guarantee, and the always-on -nostdinc flag explains why a build that works with plain cargo can fail under it.
Who is it for?
Reach for cargo-zigbuild when you are cross compiling Rust and the missing piece is a C toolchain and sysroot for the target rather than a linker you have to configure yourself, and when you can accept that the glibc version you request is a request. Do not reach for it if you need a fully static binary linked against a specific glibc, because crt-static is documented as unsupported upstream and the README points you at a musl target instead.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 9 days ago.
What is it written in?
Mainly Rust, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • rust-cross/cargo-zigbuild2,723 ★ · +51
Star history
Repository2026-09-292026-11-25
rust-cross/cargo-zigbuild2.7k2.7k

51 Stars in the last 57 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

Zig is the linker here, not the compiler

cargo-zigbuild does not replace rustc. It inserts `zig cc` in the link step so that one binary supplies the cross toolchain, which is the idea behind the post it links to, zig makes rust cross compilation just work. Everything the Rust compiler produces stays the same; what changes is the program that assembles and links it, and with it the set of host packages you need installed.

The build scripts can see what zig resolved. `CARGO_ZIGBUILD_TARGET` carries the resolved zig target triple, for example `x86_64-linux-gnu.2.36`, and a suffixed `CARGO_ZIGBUILD_TARGET_<target>` form that is always set, with the unsuffixed name populated only when a single `--target` is given. That distinction matters for build scripts that branch on the target: with two targets at once, only the per-target variable is reliable.

Two more variables exist for the unusual cases. `CARGO_ZIGBUILD_ZIG_PATH` points at the zig executable and defaults to `zig`, and `CARGO_ZIGBUILD_RUSTC_VERSION` overrides the rustc version that gets detected, which is the escape hatch when detection and reality disagree.

Three install routes, and each decides where zig comes from

The cargo route is one line:

bash
cargo install --locked cargo-zigbuild

The `--locked` flag pins the versions in Cargo.lock rather than resolving fresh ones, which matters for a tool whose job is to reproduce a link step.

The Python route installs the zig compiler as well:

bash
pip install cargo-zigbuild

That difference shapes everything downstream. Because zig arrives as the `ziglang` package, the tool has to find a Python interpreter, which is what `CARGO_ZIGBUILD_PYTHON_PATH` is for, defaulting to `python3`.

The container route bundles a whole toolchain. The images carry Rust, cargo-zigbuild and the macOS SDK already installed, on ghcr.io and on Docker Hub, so a macOS target builds from Linux without you installing an SDK:

bash
docker run --rm -it -v $(pwd):/io -w /io ghcr.io/rust-cross/cargo-zigbuild \
  cargo zigbuild --release --target x86_64-apple-darwin

Distributions are covered by the repology badge. For local use the README's own first step is still to install zig from ziglang.org, or with `pip3 install ziglang`.

The happy path is rustup target add, then cargo zigbuild

Three steps, unchanged from what any cross compilation setup asks for:

bash
rustup target add aarch64-unknown-linux-gnu
cargo zigbuild --target aarch64-unknown-linux-gnu

Add the standard library for the target with rustup, then pass the same triple to the subcommand. Nothing else changes about the invocation, which is the payoff: the command reads like `cargo build` with an extra word.

For Apple targets the SDK is found rather than configured. `SDKROOT` points at the macOS SDK and is auto-detected on macOS, `PKG_CONFIG_SYSROOT_DIR` is set to it for Apple targets so pkg-config searches the right tree, and there are per-target forms of `CMAKE_TOOLCHAIN_FILE` and `BINDGEN_EXTRA_CLANG_ARGS` for projects that generate bindings. `OHOS_NDK_HOME` is the one non-Apple entry and it is required for `ohos` targets rather than optional.

Plain `CFLAGS` and `RUSTFLAGS` are passed through, which is convenient until you use them to opt out of the tool, a problem the caveats section comes back to.

The glibc suffix is a request, not a guarantee

For a `*-gnu` target, zig picks a default glibc version that depends on the zig release, and the README pins that claim to a range: v12 to v14 default to glibc 2.28. To ask for a specific minimum, suffix the triple:

bash
cargo zigbuild --target aarch64-unknown-linux-gnu.2.17

Then the caveats, which are the most useful paragraph in the README because they describe failure modes rather than features. Without a `--target`, zig is not used at all and the command degrades to a regular `cargo build`. An invalid glibc version produces a warning from `zig cc` that cargo-zigbuild does not pass on, so a typo is silent at the cargo level.

The behaviour also does not necessarily match dynamic linking to that glibc version on the build host: 2.32 is accepted on a host that only has 2.31, while 2.33 is correctly rejected on the same host. And `-C target-feature=+crt-static` for a static glibc link is documented as unsupported because upstream `zig cc` lacks support, with a `*-musl` target offered instead. Meanwhile `RUSTFLAGS` such as `-C linker` opt out of zig entirely, while `-L path/to/files` makes zig ignore `+crt-static`.

-nostdinc is why cargo build passes and cargo zigbuild does not

cargo-zigbuild always passes the `zig cc` option `-nostdinc`, which drops standard header locations such as `/usr/include` from the search path. The README notes this is also zig's own default whenever it is configured with a `--target`, which additionally opts out of the standard system search paths. The visible effect is that the same project can build under cargo and fail here:

console
# Cannot find a header file to build:
fatal error: 'libelf.h' file not found

# Cannot find a shared library to link:
error: unable to find dynamic system library 'elf' using strategy 'no_fallback'. searched paths

The fix for headers is to hand zig a system path explicitly, `CFLAGS='-isystem /usr/include'`, with `RUSTFLAGS='-L /usr/lib64'` for libraries at `/usr/lib64`. The catch is that mixing system glibc headers with the ones zig ships produces its own diagnostics, because two definitions of the same macro meet:

rust
In file included from /usr/local/lib64/python3.13/site-packages/ziglang/lib/libunwind/src/gcc_personality_v0.c:21:
In file included from /usr/local/lib64/python3.13/site-packages/ziglang/lib/libunwind/include/unwind.h:18:
In file included from /usr/include/stdint.h:26:
In file included from /usr/include/bits/libc-header-start.h:33:
/usr/include/features.h:516:9: warning: '__GLIBC_MINOR__' macro redefined [-Wmacro-redefined]
  516 | #define __GLIBC_MINOR__ 41
      |         ^
<command line>:2:9: note: previous definition is here
    2 | #define __GLIBC_MINOR__ 37
    |

The page ends in the middle of the shared-library half of that advice, right after the name `LDFLAGS`.

The PyPI package is a maturin binary that pulls a ziglang wheel

pyproject.toml explains the pip route. The build backend is maturin with a requirement of `maturin>=1.0,<2.0`, the project is named cargo-zigbuild, and its single runtime dependency is `ziglang>=0.9.0`. The version is declared dynamic, which for a maturin build means it is taken from Cargo.toml rather than written twice, so the PyPI version and the crate version do not drift apart.

`bindings = "bin"` says the wheel ships the executable rather than a library to import. That is why a Python interpreter has to exist at run time: the binary still asks `python3` for the ziglang package unless `CARGO_ZIGBUILD_PYTHON_PATH` points somewhere else.

The Rust side of the manifest is unremarkable in the way a careful tool should be. Version 0.23.4, edition 2024, MIT, `rust-version = 1.88`, and roughly two dozen dependencies, of which the ones with a job are cargo-options and clap for the command line, rustflags for reading compiler flags, goblin and fat-macho for Mach-O work, and target-lexicon for triple parsing. `fat-macho` sits behind a default feature named `universal2`, and libc is declared only for macOS targets.

The image pins Rust 1.93.0 and Zig 0.16.0 against an MSRV of 1.88

The Dockerfile builds with `ARG RUST_VERSION=1.93.0` while Cargo.toml declares `rust-version = 1.88`, so the published minimum and the tested toolchain are five minor releases apart. Zig is pinned separately at `ARG ZIG_VERSION=0.16.0`, which sits above the v12 to v14 range the README uses when it explains the default glibc version. Read that default-glibc sentence as scoped to those releases rather than to whatever you have installed.

The install step in the image branches on the zig version, because Zig 0.14.0 renamed its tarballs from `zig-{os}-{arch}-{version}` to `zig-{arch}-{os}-{version}`. The script computes the architecture and the first two version components, picks the matching name, fetches it from ziglang.org and symlinks the binary into `/usr/local/bin/zig`.

Two build details are worth copying. Dependencies are compiled in a separate layer with only Cargo.toml and Cargo.lock present plus an empty `src/lib.rs`, so the cache survives source changes. After the full copy, `src/lib.rs` and `src/bin/cargo-zigbuild.rs` are touched by hand, because ADD preserves local timestamps and cargo would otherwise consider the cache fresh. The image also installs libclang-dev and clang for bindgen, and pulls MacOSX11.3.sdk from the phracker releases, which fixes the SDK version a macOS cross build sees at 11.3.

Editorial conclusion

Reach for cargo-zigbuild when you are cross compiling Rust and the missing piece is a C toolchain and sysroot for the target rather than a linker you have to configure yourself, and when you can accept that the glibc version you request is a request. Do not reach for it if you need a fully static binary linked against a specific glibc, because crt-static is documented as unsupported upstream and the README points you at a musl target instead. Verify first which Zig release your build will use, since the documented default of glibc 2.28 covers Zig v12 to v14 only, and read the caveats list before you put a version suffix in CI.

Frequently asked questions

What is cargo-zigbuild?

It is a cargo subcommand that compiles a Cargo project using zig as the linker, so that cross compiling needs fewer host toolchains. Install it with `cargo install --locked cargo-zigbuild` or `pip install cargo-zigbuild`, which also installs the ziglang package.

How do I cross compile a Rust project to a specific glibc version with cargo-zigbuild?

Add the version as a suffix to the `--target` value, for example `cargo zigbuild --target aarch64-unknown-linux-gnu.2.17`. The README warns that an invalid version produces a warning from `zig cc` that cargo-zigbuild does not relay, and that statically linking to a glibc version with `-C target-feature=+crt-static` is not supported.

Why does cargo zigbuild fail to find headers that plain cargo build finds?

cargo-zigbuild always passes the `zig cc` option `-nostdinc`, which excludes standard header locations such as `/usr/include`, and configuring zig with a `--target` opts out of the standard system search paths as well. The documented workaround is `CFLAGS='-isystem /usr/include'` and `RUSTFLAGS='-L /usr/lib64'`, with care not to mix system glibc headers with the ones zig provides.

Does cargo-zigbuild support building for OpenHarmony targets?

The environment variable table lists `OHOS_NDK_HOME` as the path to the OpenHarmony NDK and marks it required for `ohos` targets. The same table lists `CARGO_ZIGBUILD_ZIG_PATH`, which defaults to `zig`, and `CARGO_ZIGBUILD_CACHE_DIR` for caching zig tools and wrappers.

Official sources

  1. Issues
  2. License: MIT
  3. README
  4. Releases
  5. rust-cross/cargo-zigbuild on GitHub
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/rust-cross-cargo-zigbuild.svg)](https://hysenlabs.com/projects/rust-cross-cargo-zigbuild)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/rust-cross-cargo-zigbuild.svg)](https://hysenlabs.com/projects/rust-cross-cargo-zigbuild)

Was this useful?