zig cc: Cross-platform compile C language projects

Updated at: 2023-11-03

The Pain of C Language

C language, as a minimalist language, aligns with my philosophy. However, cross-platform compilation is inconvenient, such as compiling Linux binaries on macOS.

I've read the news that Uber has already used the Zig compiler in production, even though Zig hasn't reached version 1.0 yet. I tried compiling joker these past few days and it's truly impressive.

Zig

Since Zig hasn't reached version 1.0 yet and the package installed via brew is relatively outdated, I used nami to install zig.

nami install zig

There are several ways to use zig cc:

Replace CC

This method has the least intrusion, for example, directly specify the CC variable in the make command.

make CC="zig cc -target x86_64-linux-gnu"

Directly use zig cc

This method is suitable for situations with a small number of files.

zig cc -o joker -target x86_64-macos-none main.c run.c

Use zig build-exe

This subcommand is an encapsulation of zig cc.

zig build-exe -lc --name joker -target aarch64-macos-none -O ReleaseSafe main.c run.c

Others

zig cc also supports specifying the C standard (-std=c11). It also supports linking with the musl library, which is great. Although musl library's performance is not as good as glibc, its code implementation is clean and clear (KISS). The next version of joker will statically link with musl. For more parameters, you can check zig cc --help.


Comments