Skip to content

Instantly share code, notes, and snippets.

@eamonburns
Created February 12, 2025 00:34
Show Gist options
  • Select an option

  • Save eamonburns/fa899664aa77657ecd8d8a0609d91e26 to your computer and use it in GitHub Desktop.

Select an option

Save eamonburns/fa899664aa77657ecd8d8a0609d91e26 to your computer and use it in GitHub Desktop.
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("main.c"),
.link_libc = true,
.target = target,
.optimize = optimize,
});
// This creates another `std.Build.Step.Compile`, but this one builds an executable
// rather than a static library.
const exe = b.addExecutable(.{
.name = "hello-zig-c",
.root_source_file = translate_c.getOutput(),
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
#include <stdio.h>
int main(void) {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment