build.zig
zig构建系统主要的概念是 Module
和 Step
, 前者主要是代码承载的载体, 后者主要是个人定义自定义编译过程。
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// crate a moudle
const foo = b.createModule(.{
.root_source_file = b.path("./root.zig"),
.target = target,
.optimize = optimize,
});
// don' t do this, this will import other module
foo.addImport("foo", foo);
// define a lib
const lib_bar = b.addLibrary(.{
.root_module = foo,
});
// this with compile lib and install
b.installArtifact(lib_bar);
// define a exe
const exe_bar = b.addExecutable(.{ .root_module = foo });
b.installArtifact(exe_bar);
}
编译 C
的 hello_world, 我们创建一个空 Module
即可, 这里我们链接了 libc
,
这里例举几个关于 C
比较常用的方法
addIncludePath
添加头文件路径addCSourceFile
添加C代码addSystemIncludePath
添加库路径linkSystemLibrary
链接库addObjectFile
添加 object
文件const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// define a empty module
const hello_module = b.createModule(.{
.link_libc = true,
.target = target,
.optimize = optimize,
});
hello_module.addCSourceFiles(.{ .files = &.{"./hello.c"} });
const exe_hello = b.addExecutable(.{ .name = "hello", .root_module = hello_module });
b.installArtifact(exe_hello);
}
dependency
@ref: https://github.com/Not-Nik/raylib-zig
通过 zig fetch
拉取包, 默认自动保存至 build.zig.zon
内, 通过 Build.dependency
获取一个 Dependency
类型的句柄,通过 Dependency.module(<module_name>)
获取 Module
. Dependency.artifact
获取 Step.Compile
可被 linkLibrary
接受。