Skip to content

Instantly share code, notes, and snippets.

@maatthc
Created March 19, 2026 11:46
Show Gist options
  • Select an option

  • Save maatthc/72adfc6e45057b3db13d650fa530a175 to your computer and use it in GitHub Desktop.

Select an option

Save maatthc/72adfc6e45057b3db13d650fa530a175 to your computer and use it in GitHub Desktop.
Compiling C with Zig 0.15.2 build system - with support to compile_commands.json for ClangD LSP
// A simple zig module to generate compile_commands.json, used by clangd LSP server
// https://github.com/the-argus/zig-compile-commands
const zcc = @import("compile_commands");
const std = @import("std");
pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
var clangd = try std.ArrayList(*std.Build.Step.Compile).initCapacity(b.allocator, 5);
defer clangd.deinit(b.allocator);
const root_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true });
root_module.addCSourceFile(.{ .file = b.path("src/main.c"), .flags = &.{
"-Wall",
"-Wextra",
"-Werror",
} });
const exe = b.addExecutable(.{ .name = "c-project", .root_module = root_module });
b.installArtifact(exe);
const run_exe = b.addRunArtifact(exe);
const step_run = b.step("run", "Run the C project");
step_run.dependOn(&run_exe.step);
run_exe.step.dependOn(b.getInstallStep());
clangd.append(b.allocator, exe) catch @panic("Out of memory");
_ = zcc.createStep(b, "compile_commands_json", clangd.toOwnedSlice(b.allocator) catch @panic("Out of memory"));
// Add tests using https://github.com/dokwork/zrunner?tab=readme-ov-file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment