#Compiling C Code Using Zig Build System

1 messages · Page 1 of 1 (latest)

toxic flare
#

In Zig 0.11, I could do something like this in build.zig to compile C code:

const std = @import("std");

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

    const exe = b.addExecutable(.{
        .name = "scratchpad",
        .root_source_file = .{ .path = "src/main.c" },
        .target = target,
        .optimize = optimize,
    });
    exe.linkLibC();

    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);

    run_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

main.c is a trivial hello world program. This works in zig 0.11.0, but in Zig 0.12.0 it produces an error:

PS> zig build
install
└─ install scratchpad
   └─ zig build-exe scratchpad Debug native 1 errors
src\main.c:1:1: error: expected type expression, found 'invalid bytes'
#include <stdio.h>
^
src\main.c:1:1: note: invalid byte: '#'
error: the following command failed with 1 compilation errors:
C:\Users\user\.zvm\0.12.0\zig.exe build-exe -ODebug -Mroot=C:\Users\user\Documents\Projects\Zig\scratchpad\src\main.c -lc --cache-dir C:\Users\user\Documents\Projects\Zig\scratchpad\zig-cache --global-cache-dir C:\Users\user\AppData\Local\zig --name scratchpad --listen=-
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install scratchpad transitive failure
   └─ zig build-exe scratchpad Debug native 1 errors
error: the following build command failed with exit code 1:
C:\Users\user\Documents\Projects\Zig\scratchpad\zig-cache\o\7ade1bc164d3d05fb50cb6f81d972220\build.exe C:\Users\user\.zvm\0.12.0\zig.exe C:\Users\user\Documents\Projects\Zig\scratchpad C:\Users\user\Documents\Projects\Zig\scratchpad\zig-cache C:\Users\user\AppData\Local\zig --seed 0x5df2ec03 -Z5b70bda4dbc5aed8
}
}
#

I'm assuming something changed in the Zig build system that I'm not quite getting, as zig cc still works. What am I missing?