Skip to content

Zig as a C Compiler

Here’s something that surprises people: the zig download you already have is also a complete C and C++ compiler. zig cc is Clang underneath (version 22.1.8 in Zig 0.17.1), plus the headers and C libraries for dozens of operating systems and CPUs. That last part is the reason some teams use Zig only for this: building C for another platform usually means installing a separate cross-compiler toolchain, and with zig cc it’s one flag. The official overview calls Zig “also a C compiler” for this reason. This page shows how to use it, even if you never write a line of Zig.

Take the smallest C program there is:

hello.c
#include <stdio.h>
int main(void) {
printf("Hello from C, built by zig cc!\n");
return 0;
}

Compile and run it exactly as you would with gcc or clang:

Terminal window
zig cc hello.c -o hello
./hello

You’ll see:

Hello from C, built by zig cc!

zig cc takes the usual compiler options: -o for the output name, -O2 to optimize, -Wall for warnings, -I for include folders, -l for libraries, -c to only compile, and so on. For C++ there’s zig c++:

hello.cpp
#include <iostream>
int main() {
std::cout << "Hello from C++, built by zig c++!\n";
}
Terminal window
zig c++ hello.cpp -o hellocpp
./hellocpp

You’ll see:

Hello from C++, built by zig c++!

Now the fun part. Add -target and the same command builds for a different operating system or CPU. The file command tells you what you got:

Terminal window
zig cc hello.c -o hello
file hello
zig cc -target x86_64-windows-gnu hello.c -o hello.exe
file hello.exe
zig cc -target aarch64-linux-musl hello.c -o hello-arm
file hello-arm
zig cc -target aarch64-macos hello.c -o hello-mac
file hello-mac

On our x86-64 Linux machine, you’ll see:

hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, with debug_info, not stripped
hello.exe: PE32+ executable (console) x86-64, for MS Windows, 7 sections
hello-arm: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not stripped
hello-mac: Mach-O 64-bit arm64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|NO_REEXPORTED_DYLIBS|PIE>

A Windows program, a 64-bit ARM Linux program (the kind that runs on a Raspberry Pi 4 with a 64-bit OS) and an Apple Silicon Mac program, all from one Linux machine with nothing else installed. You can’t run them here, of course; copy them to the right machine. (The Windows build also leaves a hello.pdb next to the .exe: that’s Windows debug info.)

A target is written cpu-os-abi:

  • cpu: x86_64, aarch64, arm, riscv64, wasm32, …
  • os: linux, windows, macos, freebsd, wasi, …
  • abi (mostly for Linux): gnu means glibc, musl means the musl C library.

zig targets prints every combination Zig knows about. It’s long. The same target names work for Zig code too (Zig Cross-Compiling).

There’s a classic Linux headache: you build on a new system, copy the program to an older server, and it refuses to start with version 'GLIBC_2.34' not found. Your program was linked against the newer glibc on your machine.

With zig cc you can pick the glibc version to build against by adding it to the target (the 0.17.0 release notes say glibc 2.44 is now available when cross-compiling):

Terminal window
zig cc hello.c -o hello
objdump -T hello | grep GLIBC
zig cc -target x86_64-linux-gnu.2.28 hello.c -o hello-old
objdump -T hello-old | grep GLIBC

You’ll see:

0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.34) __libc_start_main
0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.2.5) puts
0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.2.5) __libc_start_main
0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.2.5) printf

The first build needs glibc 2.34 or newer (because this machine has it). The second only asks for symbols that exist in old glibc versions, so it runs on systems with glibc 2.28 and later. (Clang turned the simple printf into a faster puts in the first build; that’s normal and harmless.)

A nice surprise: undefined behavior gets caught

Section titled “A nice surprise: undefined behavior gets caught”

Here’s a C program with a bug. Adding 1 to the largest int is undefined behavior in C: the compiler may do anything.

ub.c
#include <limits.h>
#include <stdio.h>
int main(void) {
volatile int big = INT_MAX;
int result = big + 1; /* signed overflow: undefined behavior in C */
printf("result = %d\n", result);
return 0;
}

gcc ub.c && ./a.out quietly prints result = -2147483648. With zig cc and no -O flag:

Terminal window
zig cc ub.c -o ub
./ub

You’ll see something like:

thread 24123 panic: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
ub.c:6:22: 0x10215b1 in main (ub.c)
int result = big + 1; /* signed overflow: undefined behavior in C */
^

In unoptimized builds, zig cc turns on Clang’s undefined behavior sanitizer, so the program stops right at the bug with a Zig-style panic message, the same way Zig code stops on integer overflow (Zig Integer Overflow). With -O2 the checks are off and you get the silent wrong answer again.

This also explains a common report: “my C project works with gcc but crashes with zig cc”. Often that’s zig cc finding a real bug that was there all along. Fix the bug if you can. If you can’t right now (third-party code, say), -fno-sanitize=undefined turns the checks off.

Most C build systems let you choose the compiler with the CC variable (and CXX for C++). So zig cc plugs straight into an existing project. With this Makefile:

CC ?= cc
hello: hello.c
$(CC) -O2 -o hello hello.c

you can run:

Terminal window
make CC="zig cc"
make CC="zig cc -target aarch64-linux-musl"

You’ll see:

zig cc -O2 -o hello hello.c
zig cc -target aarch64-linux-musl -O2 -o hello hello.c

The second one is a cross-compile of an unchanged make project. (Run make clean or delete hello between the two, or make will say it’s up to date.) CMake, Meson and autotools projects usually accept the same CC="zig cc" CXX="zig c++" trick. Bigger projects can trip over details, like build scripts that expect a compiler to be a single word, so treat this as “often works” rather than “always works”.

If you’re starting a project, you can also skip make and use build.zig, even for a program that’s 100% C. A module without a root_source_file holds only C:

const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
mod.addCSourceFile(.{ .file = b.path("hello.c"), .flags = &.{"-Wall"} });
const exe = b.addExecutable(.{ .name = "hello", .root_module = mod });
b.installArtifact(exe);
}

Then zig build builds it, and zig build -Dtarget=x86_64-windows-gnu -Doptimize=fast builds an optimized Windows version. You get caching and cross-compiling with nothing to install except Zig. Zig Calling C Libraries has more on C files in build.zig.

To be fair about it:

  • It’s Clang, not GCC. Code that depends on GCC-only extensions or GCC-specific flags may need changes.
  • macOS targets work for plain C and the system C library, but programs that use Apple frameworks (like Cocoa) need the macOS SDK, which Zig doesn’t ship.
  • The Windows target uses the MinGW-w64 headers and libraries (that’s the -gnu in x86_64-windows-gnu), not Microsoft’s MSVC ones. Most portable C code doesn’t care; code written specifically for MSVC might.
  • Cross-compiled programs can’t be run on your machine without an emulator or the real hardware.

Compile hello.c for 64-bit RISC-V Linux with musl, and check the result with file.

Hint: the CPU name is riscv64.

Show solution
Terminal window
zig cc -target riscv64-linux-musl hello.c -o hello-rv
file hello-rv

You’ll see:

hello-rv: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linked, with debug_info, not stripped

Same source, same command shape, different -target.

This program reads past the end of an array. Build it with zig cc (no -O) and run it. What happens? Then try zig cc -O2.

oops.c
#include <stdio.h>
int main(void) {
int numbers[3] = {1, 2, 3};
volatile int i = 3;
printf("%d\n", numbers[i]);
return 0;
}

Hint: reading numbers[3] is out of bounds, which is undefined behavior.

Show solution
Terminal window
zig cc oops.c -o oops
./oops

You’ll see something like:

thread 24317 panic: index 3 out of bounds for type 'int[3]'
oops.c:6:20: 0x10215c4 in main (oops.c)
printf("%d\n", numbers[i]);
^

The undefined behavior sanitizer catches the bad index. With -O2 the check is gone and the program prints whatever happens to be in memory after the array.

You have a Makefile that uses $(CC). Write the command that builds it for 64-bit Windows with zig cc.

Hint: pass CC on the make command line, with the target inside the quotes.

Show solution
Terminal window
make CC="zig cc -target x86_64-windows-gnu"

If the Makefile names the output hello, you may want to add .exe yourself; Windows doesn’t need it to run the file, but people expect it.

  • zig cc and zig c++ are a full Clang with C libraries and headers for many platforms, inside the normal Zig download.
  • They accept the usual gcc/clang flags (-o, -O2, -Wall, -I, -l).
  • -target cpu-os-abi cross-compiles, e.g. x86_64-windows-gnu, aarch64-linux-musl, aarch64-macos.
  • -musl targets give static Linux programs; x86_64-linux-gnu.2.28 picks an older glibc to stay compatible.
  • Unoptimized builds turn on the undefined behavior sanitizer, which stops C bugs with a clear panic.
  • Plug it into existing builds with CC="zig cc", or compile C from build.zig.

Next up: Zig Exporting to C, writing a library in Zig that C programs can call.

Quick check

1. What is zig cc underneath?
2. Which command builds hello.c for 64-bit ARM Linux as a static program?
3. A C program works with gcc but panics with "signed integer overflow" when built with zig cc. Why?
4. How do you make an existing make project use zig cc?