From 9deacd47b690a3e7ae1870db19dc91d77e2b4eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=C3=A2jinzhongjia?= Date: Sat, 9 Dec 2023 21:49:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=A4=E5=8F=89=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- learn/engineering/build-system.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/learn/engineering/build-system.md b/learn/engineering/build-system.md index 3d82bb61..4b53ea44 100644 --- a/learn/engineering/build-system.md +++ b/learn/engineering/build-system.md @@ -431,7 +431,28 @@ pub fn build(b: *std.Build) void { ## 交叉编译 -TODO +得益于 LLVM 的存在,zig 支持交叉编译到任何 LLVM 的目标代码,zig 可以很方便的处理交叉编译,只需要指定好恰当的 target 即可。 + +关于所有的 target,可以在此处 [查看](https://ziglang.org/documentation/master/#Targets)。 + +最常用的一个 target 设置可能是 `b.standardTargetOptions`,它会允许读取命令行输入来决定构建目标 target,它返回一个 [`CrossTarget`](https://ziglang.org/documentation/master/std/#A;std:zig.CrossTarget)。 + +如果需要手动指定一个 target,可以手动构建一个 `CrossTarget` 传递给构建(`addExecutable` 和 `addStaticLibrary`),如: + +```zig +var target: std.zig.CrossTarget = .{ + .cpu_arch = .x86_64, + .os_tag = .freestanding, + .abi = .none, +}; + +const exe = b.addExecutable(.{ + .name = "zig", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, +}); +``` ## `embedFile`