Day 1: Installation and Hello World
Welcome to Day 1 of my 1000 Days of Zig challenge! Today, I’m setting up the environment and writing my first Zig program.Truth be told this is like my month four of doing Zig, I started back in December, take this Day 1 story with caution.
Installing Zig
Zig is available on multiple platforms. The easiest way to get started is to download a pre-built binary from the Zig downloads page.
On macOS (using Homebrew):
brew install zig
On Linux:
# Download the latest master build
wget https://ziglang.org/builds/zig-linux-x86_64-<version>.tar.xz
tar xf zig-linux-x86_64-<version>.tar.xz
export PATH="$PWD/zig-linux-x86_64-<version>:$PATH"
Verify Installation:
zig version
Hello, World!
Let’s write the classic “Hello, World!” program in Zig. Create a file named hello.zig:
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, World!\n", .{});
}
Running the Program:
zig run hello.zig
Output:
Hello, World!
Understanding the Code
Let’s break down what’s happening:
const std = @import("std");- Imports the Zig standard library and assigns it to the constantstdpub fn main() void- Declares the main function that returns nothing (void). Zig is still under heavy development just recently Zig 0.16 was released, which introduced a number of big changes, Writergate was also part of the releasestd.debug.print()- Uses the standard library’s debug print function.{}- An empty anonymous struct literal, used here as the format arguments
Using Zig’s Build System
Zig comes with a powerful build system. Let’s create a proper project:
mkdir hello_world
cd hello_world
zig init-exe
This creates a basic project structure with:
build.zig- Build configurationsrc/main.zig- Main source file
Building the Project:
zig build
Running the Built Executable:
zig build run
Cross-Compilation
One of Zig’s killer features is easy cross-compilation. Let’s compile our hello world for Windows from macOS/Linux:
zig build-exe hello.zig -target x86_64-windows
Or for WebAssembly:
zig build-exe hello.zig -target wasm32-wasi
What I Learned Today
- Zig installation is straightforward
- The language syntax is clean and explicit
- The build system is integrated and powerful
- Cross-compilation is incredibly easy
- No hidden allocations or control flow
Looking Ahead
Tomorrow, I’ll look into Ziglings and do about 10 exercises. Stay tuned for Day 2!
References
Comments