Creating a Simple Zig Project

Siva
2 min readSep 16, 2024

--

Zig is a modern programming language designed with a focus on simplicity, safety, and performance. In this guide, we’ll walk through the steps to create a simple Zig project, modify the source code, and run the project.

Step 1: Create a New Directory and Navigate Into It

First, let’s create a new directory for our project and navigate into it.

mkdir sample-project && cd sample-project

Step 2: Initialize a New Zig Project

Next, we’ll initialize a new Zig project using the zig init command. This command will create a basic project structure with a build.zig file and a src directory containing a main.zig file.

zig init

Step 3: Build the Project

Now, let’s build the project using the zig build command. This command will compile the project based on the configuration in build.zig.

zig build

Step 4: Build and Run the Project

To compile and run the project, use the zig build run command.

zig build run

This command will compile and run the project, and you should see the default output from the main.zig file.

Step 5: Navigate to the “src” Directory

Next, navigate to the src directory where the main source file is located.

cd src/

Step 6: Modify the “main.zig” File

Open the main.zig file in your preferred text editor (e.g., Vim) and modify it as required. For this example, let’s modify the file to print “Hello, Zig!”.

const std = @import(“std”);

pub fn main() void {
const stdout = std.io.getStdOut().writer();
stdout.print(“Hello, Zig!\n”, .{}) catch {};
}

Step 7: Rebuild and Run the Project

Navigate back to the root directory of the project and rebuild it using the zig build run command.

cd ..
zig build run

This will compile and run the modified project, and you should see the output:

Hello, Zig!

Conclusion

That’s it! You’ve created a simple Zig project, modified the source code, and rebuilt the project to see the changes. Zig is a powerful language with a lot of potential, and this guide should give you a good starting point for exploring its features.

Reference:

  1. https://ziglang.org/documentation/0.13.0/#Hello-World
  2. https://zig.guide/getting-started/hello-world

--

--