When I attempt building a project containing both Zig and C for FreeBSD or OpenBSD on my Linux system, it appears that Zig can’t find system headers like stdio.h or stdint.h. Compiling for Linux, macOS and Windows all work for this example.
src/main.c:
#include <stdio.h>
#include <stdint.h>
uint32_t mul(uint32_t a, uint32_t b) {
return a * b;
}
src/main.zig:
const std = @import("std");
extern fn mul(a: u32, b: u32) u32;
pub fn main() !void {
std.debug.print("From C: {d}\n", .{mul(420, 69)});
}
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(.{
.name = "ctest",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.addCSourceFile(.{
.file = .{ .path = "src/main.c" },
.flags = &[_][]const u8{},
});
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);
}
For some reason though, FreeBSD and OpenBSD both fail to build due to “error: ‘stdio.h’ file not found”. Can you not cross-compile to them by default or an I doing something incorrect here?