Menu

Zig a Zig Ah?

14th August 2024 - python, zig
Zig a Zig Ah?

There’s a bit of the old and proverbial buzz going around zig at the moment and after watching Loris Cro make the case for ZIG as a Multi-OS build system (albeit using containers). I have done some C/C++ in my varied career and it was always a massive pain. I came into programming directly into the internet era, where we had JAVA and Javascript as well as Perl and PHP. C/C++ has always been a rats nest of libraries, and tool chains to find C libraries and libraries that didn’t work on particular platforms because of dependencies that override, yadda yadda. And Windows is always a third class citizen.

It still looks terrible, and handmade, and annoying. But we seem to have some modern sensibility for development libraries and development environments, which is promising.

At time of writing, it is only on version 0.14, so I cranked up a version of Rocky 9.3 on Hyper-V and went to install Zig..

Unfortunately, there is no package available for Rocky. I sigh. But undeterred, I continue in Rocky instead of falling back to Ubuntu 22.

I went to the zig page and copied the URL and downloaded the tar into my rocky console.

wget https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.1052+d6f997259.tar.xz

Then I untar it and add it to the path.

tar xf zig-linux-x86_64-0.14.0-dev.1052+d6f997259.tar.xz
echo 'export PATH="$HOME/zig-linux-x86_64-0.14.0-dev.1052+d6f997259:$PATH"' >> ~/.bashrc

And then a quick restart of bash and we’re good.

exec bash

Typing zig version will show my version as 0.14.0-dev.1052+d6f997259

I create a file called hello.zig

const std = @import("std");

pub fn main() void {
        std.debug.print("Hello, {s}!\n",.{"Zig"});
}

Then zig run hello.zig and we get our hello world program.

Test First

There is an assertion engine built into the zig standard.

const std = @import("std");

test "expect this to fail" {
    try std.testing.expect(false);
}

test "expect this to succeed" {
    try std.testing.expect(true);
}

test "expect addTwo adds two to 51" {
    try std.testing.expect(addTwo(51) == 53);
}

test addTwo {
    try std.testing.expect(addTwo(51) == 53);
}

fn addTwo(number: i32) i32 {
    return number + 2;
}

Python Platform Wheels

Wheels are a packaging concept for Python to allow packages to be created that can contain and target specific platforms and python types.

With Zig, the multi-platform problem could be solved by leveraging the zig build chain in the CI process.

Leave a Reply