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.
Compile a C program
Section titled “Compile a C program”Take the smallest C program there is:
#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:
zig cc hello.c -o hello./helloYou’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++:
#include <iostream>
int main() { std::cout << "Hello from C++, built by zig c++!\n";}zig c++ hello.cpp -o hellocpp./hellocppYou’ll see:
Hello from C++, built by zig c++!Cross-compiling: one flag
Section titled “Cross-compiling: one flag”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:
zig cc hello.c -o hellofile hello
zig cc -target x86_64-windows-gnu hello.c -o hello.exefile hello.exe
zig cc -target aarch64-linux-musl hello.c -o hello-armfile hello-arm
zig cc -target aarch64-macos hello.c -o hello-macfile hello-macOn 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 strippedhello.exe: PE32+ executable (console) x86-64, for MS Windows, 7 sectionshello-arm: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not strippedhello-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):
gnumeans glibc,muslmeans 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).
Targeting an older glibc
Section titled “Targeting an older glibc”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):
zig cc hello.c -o helloobjdump -T hello | grep GLIBC
zig cc -target x86_64-linux-gnu.2.28 hello.c -o hello-oldobjdump -T hello-old | grep GLIBCYou’ll see:
0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.34) __libc_start_main0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.2.5) puts0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.2.5) __libc_start_main0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.2.5) printfThe 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.
#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:
zig cc ub.c -o ub./ubYou’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.
Use it with make and other build tools
Section titled “Use it with make and other build tools”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.cyou can run:
make CC="zig cc"make CC="zig cc -target aarch64-linux-musl"You’ll see:
zig cc -O2 -o hello hello.czig cc -target aarch64-linux-musl -O2 -o hello hello.cThe 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”.
Or let zig build compile the C
Section titled “Or let zig build compile the C”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.
What zig cc doesn’t do
Section titled “What zig cc doesn’t do”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
-gnuinx86_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.
Practice
Section titled “Practice”1. Build and inspect
Section titled “1. Build and inspect”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
zig cc -target riscv64-linux-musl hello.c -o hello-rvfile hello-rvYou’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 strippedSame source, same command shape, different -target.
2. Catch the bug
Section titled “2. Catch the bug”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.
#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
zig cc oops.c -o oops./oopsYou’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.
3. Cross-compile a make project
Section titled “3. Cross-compile a make project”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
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 ccandzig 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-abicross-compiles, e.g.x86_64-windows-gnu,aarch64-linux-musl,aarch64-macos.-musltargets give static Linux programs;x86_64-linux-gnu.2.28picks 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 frombuild.zig.
Next up: Zig Exporting to C, writing a library in Zig that C programs can call.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- zig cc in the 0.17.0 release notes: the current Clang version.
- In-depth Overview: Zig is also a C compiler: the official overview of
zig ccand cross-compiling C. - glibc 2.44 and bundled headers (0.17.0 release notes): glibc 2.44 support, and the Linux, macOS and MinGW-w64 headers that ship with Zig 0.17.
- Clang command line reference: every flag
zig ccpasses through to Clang.