Zig原生支持跨平台编译(Cross-compilation)
Zig原生支持跨平台编译(Cross-compilation),可以在macOS上直接编译出Windows、Linux等平台的可执行文件,不必为每个平台单独安装一套交叉编译工具链。
1. 支持哪些平台?
| 目标平台 | CPU架构 | Zig Target示例 |
|---|---|---|
| macOS | Apple Silicon | aarch64-macos |
| macOS | Intel | x86_64-macos |
| Linux | x86_64 | x86_64-linux-gnu |
| Linux | ARM64 | aarch64-linux-gnu |
| Windows | x86_64 | x86_64-windows |
| Windows | ARM64 | aarch64-windows |
| WebAssembly | WASM32 | wasm32-wasi |
2. 在Mac上编译不同平台
macOS ARM64适用于M系列芯片
zig build-exe hello.zig -target aarch64-macos
Windows x64生成Windows可执行文件
zig build-exe hello.zig -target x86_64-windows
Linux x64使用GNU ABI
zig build-exe hello.zig -target x86_64-linux-gnu
Linux x64静态链接使用musl,减少目标机器的动态库依赖
zig build-exe hello.zig -target x86_64-linux-musl