|home| |posts| |projects| |cv| |bookmarks| |github|

Cross Compile C and Cpp Programs With Zig

Based on this post by Andrew Kelley.

Install zig

wget https://ziglang.org/download/0.9.1/zig-linux-x86_64-0.9.1.tar.xz
tar xf zig-linux-x86_64-0.9.1.tar.xz

Compile your program

Example program:

#include <stdio.h>

int main() {
    printf("hello\n");
    return 0;
}

Add zig to your PATH:

export PATH=$PATH:$PWD/zig-linux-x86_64-0.9.1

Now you can start compiling your program for different targets:

zig cc -o example-x86_64 example.c -target x86_64-linux-musl
file example-x86_64
example-x86_64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped
zig cc -o example-aarch64 example.c -target aarch64-linux-musl
file example-aarch64
example-aarch64: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, stripped
zig cc -o example-mips example.c -target mips-linux-musl
file example-mips
example-mips: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), statically linked, stripped

And many more, see zig targets | jq .libc