Zig build package and bindings for GLFW
  • C 89.9%
  • Objective-C 7.6%
  • Zig 2.5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Chris Heyes 2450fe3c41
Update main.yml
Re-enable macos CI
2026-10-22 00:00:08 +01:00
.github/workflows Update main.yml 2026-10-22 00:00:08 +01:00
libs/glfw files from monorepo @ 613d847104a072fc1d4442f58d5f50b3f9c3f27c 2024-11-03 21:45:51 +00:00
src Support latest vulkan-zig 2026-10-15 00:33:11 +02:00
.gitignore - Migrate to translate-c from @cTranslate 2026-10-15 00:33:11 +02:00
build.zig Fix to work with zig version 0.17.0-dev.1098+56e313b28 2026-10-15 00:33:11 +02:00
build.zig.zon Bump minimum zig version to 0.17.0 2026-10-15 00:34:01 +02:00
LICENSE files from monorepo @ 613d847104a072fc1d4442f58d5f50b3f9c3f27c 2024-11-03 21:45:51 +00:00
README.md zig v0.16 (#51) 2026-04-22 21:48:09 +01:00

zglfw

Zig build package and bindings for GLFW 3.4

Getting started

Example build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{ ... });

    const zglfw = b.dependency("zglfw", .{
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("zglfw", zglfw.module("root"));

    if (target.result.os.tag != .emscripten) {
        exe.root_module.linkLibrary(zglfw.artifact("glfw"));
    }

    b.installArtifact(exe);
}

Now in your code you may import and use zglfw:

const glfw = @import("zglfw");

pub fn main() !void {
    try glfw.init();
    defer glfw.terminate();

    const window = try glfw.createWindow(600, 600, "zig-gamedev: minimal_glfw_gl", null, null);
    defer glfw.destroyWindow(window);

    // or, using the equivalent, encapsulated, "objecty" API:
    const window = try glfw.Window.create(600, 600, "zig-gamedev: minimal_glfw_gl", null, null);
    defer window.destroy();

    // setup your graphics context here

    while (!window.shouldClose()) {
        glfw.pollEvents();

        // render your things here

        window.swapBuffers();
    }
}

See zig-gamedev samples for more complete usage examples.

Usage with Vulkan

To match types from zglfw functions and Vulkan library import_vulkan option may be used. When using this option vulkan import must be provided to the root module.

Example build.zig with vulkan-zig:

const vulkan_headers = b.dependency("vulkan_headers", .{});
const vulkan = b.dependency("vulkan", .{
    .registry = vulkan_headers.path("registry/vk.xml"),
}).module("vulkan-zig");
exe.root_module.addImport("vulkan", vulkan);

const zglfw = b.dependency("zglfw", .{
    .target = target,
    .optimize = optimize,
    .import_vulkan = true,
});

const zglfw_mod = zglfw.module("root");
zglfw_mod.addImport("vulkan", vulkan);
exe.root_module.addImport("zglfw", zglfw_mod);

if (target.result.os.tag != .emscripten) {
    exe.root_module.linkLibrary(zglfw.artifact("glfw"));
}