zig: write and cross-compile maintainable robust, optimal, and reusable software.
https://ziglang.org/| Installer Source| Releases (json) (tab)
zig: write and cross-compile maintainable robust, optimal, and reusable software.
https://ziglang.org/| Installer Source| Releases (json) (tab)
To update or switch versions, run webi zig@stable
(or @v0.9
, @beta
, etc).
These are the files / directories that are created and/or modified with this install:
~/.config/envman/PATH.env
~/.local/opt/zig
zig
is two things:
- A drop-in cross-compiling toolchain for C and C++ (and Rust and CGo for that matter).
- A general purpose systems programming language, like C, but intentional, and with benefit of hindsight.
(and also a nod to Zero Wing)
Philosophy:
You can use Zig as a drop-in C compiler.
zig cc -o ./hello main.c
zig c++ -o ./hello++ main.cpp
And you can cross-compile effortlessly:
zig cc -o ./hello.exe main.c -target x86_64-windows-gnu
zig c++ -o ./hello.exe main.cpp -target x86_64-windows-gnu
mkdir -p ./zig-hello/
pushd ./zig-hello/
build.zig
zig init-exe
hello.exe
for Windows from MacOS or Linuxzig build-exe src/main.zig --name hello -target x86_64-windows-gnu
zig build-exe src/main.zig --name hello -target x86_64-linux-musl
zig build-exe src/main.zig --name hello-arm -target aarch64-linux-musl
zig build-exe src/main.zig --name hello-macos -target x86_64-macos-gnu
zig build-exe src/main.zig --name hello-m1 -target aarch64-macos-gnu
zig targets | jq -r '.libc[]'
Here's a few of the common targets:
aarch64-linux-musl
aarch64-windows-gnu
aarch64-macos-gnu
thumb-linux-musleabihf
wasm32-wasi-musl
x86_64-linux-musl
x86_64-windows-gnu
x86_64-macos-gnu
Create a zig-cc-{ARCH-OS}
and zig-cpp-{ARCH-OS}
wrappers:
cat << EOF >> ~/.local/bin/zig-cc-x86_64-windows-gnu
#!/bin/sh
set -e
set -u
"\${HOME}/.local/opt/zig/zig" cc -target x86_64-windows-gnu \$@
EOF
chmod a+x ~/.local/bin/zig-cc
cat << EOF >> ~/.local/bin/zig-cpp-x86_64-windows-gnu
#!/bin/sh
set -e
set -u
"\${HOME}/.local/opt/zig/zig" c++ -target x86_64-windows-gnu \$@
EOF
chmod a+x ~/.local/bin/zig-cpp
Set the CC
, CPP
and ZIGTARGET
ENVs. For example:
#export ZIGTARGET="x86_64-windows-gnu"
export CC="zig-cc-x86_64-windows-gnu"
export CPP="zig-cpp-x86_64-windows-gnu"
Install the correpsonding Rust toolchains:
rustup target install x86_64-apple-darwin
rustup target install x86_64-unknown-linux-musl
rustup target install aarch64-unknown-linux-musl
rustup target install x86_64-pc-windows-gnu
You may need to also specifically set the linker. For example, with Rust's
~/.cargo/config.toml
:
[target.x86_64-apple-darwin]
linker = "zig-cc-x86_64-macos-gnu"
[target.x86_64-unknown-linux-musl]
linker = "zig-cc-x86_64-unknown-linux-musl"
[target.aarch64-unknown-linux-musl]
linker = "zig-cc-aarch64-unknown-linux-musl"
[target.x86_64-pc-windows-gnu]
linker = "zig-cc-x86_64-windows-gnu"
~/.local/bin/zig-create-crossies
:
#!/bin/bash
set -e
set -u
my_targets="$(zig targets | jq -r '.libc[]' | sort -u)"
for my_target in $my_targets; do
cat << EOF >> "${HOME}/.local/bin/zig-cc-${my_target}"
#!/bin/sh
set -e
set -u
"\${HOME}/.local/opt/zig/zig" cc -target ${my_target} \$@
EOF
chmod a+x "${HOME}/.local/bin/zig-cc-${my_target}"
done
for my_target in $my_targets; do
cat << EOF >> "${HOME}/.local/bin/zig-cpp-${my_target}"
#!/bin/sh
set -e
set -u
"\${HOME}/.local/opt/zig/zig" c++ -target ${my_target} \$@
EOF
chmod a+x "${HOME}/.local/bin/zig-cpp-${my_target}"
done
See also:
See the section above about Rust.
It's almost exactly the same.
See also: