Skip to content

Cross Compilation

buildx.zig supports cross-compilation via the cross field in ProjectOptions.

Basic Usage

zig
const std = @import("std");
const buildx = @import("buildx");

pub fn build(b: *std.Build) void {
    _ = buildx.project(b, .{
        .name = "myapp",
        .root = "src/main.zig",
        .cross = .{
            .targets = buildx.targets.all(),
        },
    });
}

Run with zig build cross.

Target Presets

PresetTargetsDescription
targets.all()216All OS + all architectures
targets.desktop()6Windows/Linux/macOS, x86_64 + aarch64
targets.linux()16Linux, all architectures
targets.windows()4Windows, x86/x64/ARM/AArch64
targets.macos()2macOS, x86_64 + aarch64
targets.arm()24ARM/AArch64 across all OS
targets.riscv()2RISC-V 32/64 on Linux
targets.freestanding()8No OS, all architectures
targets.wasm()2WebAssembly 32/64 on WASI
targets.allArchitectures()21All CPU architectures

Custom Target List

zig
_ = buildx.project(b, .{
    .name = "myapp",
    .root = "src/main.zig",
    .cross = .{
        .targets = &.{
            .{ .cpu_arch = .x86_64, .os_tag = .linux },
            .{ .cpu_arch = .aarch64, .os_tag = .linux },
            .{ .cpu_arch = .x86_64, .os_tag = .windows },
        },
        .step_name = "release-cross",
        .step_desc = "Build release binaries for Linux and Windows",
    },
});

Standalone crossCompile

You can also call crossCompile directly on an existing compile step:

zig
const exe = buildx.project(b, .{
    .name = "myapp",
    .root = "src/main.zig",
});

buildx.crossCompile(b, exe, .{
    .targets = buildx.targets.desktop(),
});

Output

Cross-compiled artifacts are placed in zig-out/bin/ with triple suffixes:

zig-out/bin/myapp-x86_64-linux
zig-out/bin/myapp-aarch64-linux
zig-out/bin/myapp-x86_64-windows.exe

Released under the MIT License.