跳至正文
Wener Notes

Zig

约 1 分钟阅读
    • MIT, Zig
  • Why
    • 与 C 交互非常好, 可直接导入 C 头文件使用
    • 可与 C 的编译后对接进行混合编译
    • stdlib 设计上不依赖 libc - wasm 更小
    • 内置 libc - 支持交叉编译
      • ARCH: aarch64,aarch64_6e,armeb,arm,i386,mips64el,mips64,mipsel,mips,powerpc64le,powerpc64,powerpc,riscv64,s390x,sparc,sparcv9,x86_64
      • {ARCH}-{linux,windows}-{musl,gnu}{,eabi,eabihf,abi64,abin32}
      • wasm32-freestanding-musl
      • 40+
      • linux 3.16+, macOS 10.13+, Windows 8.1+
    • 内置 clang - zig cc
    • 动态编译路径
  • 参考
bash
docker run --rm -it -v $PWD:/host -w /host wener/zig
hello.zig
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}
hello.zig
const print = @import("std").debug.print;

pub fn main() void {
    print("Hello, world!\n", .{});
}
bash
zig build-exe hello.zig
# 交叉编译
zig targets # 所有 targets
zig build-exe -target arm-linux hello.zig

zig build-exe -target arm-linux-musleabi -mcpu arm1176jzf_s hello.zig
zig build-exe -target arm-linux-gnueabi -mcpu arm1176jzf_s hello.zig

# hello 约 4.4k
zig build-exe --strip -O ReleaseSmall hello.zig
  • flags
    • -fsingle-threaded
    • -mcpu=arm926ej_s+soft_float
    • -l[lib], --library [lib] - 在需要时 link 系统库
    • -L[d], --library-directory [d] - 添加 库 目录

C Interop

  • --verbose-cc 查看 cc 命令
hello.c
#include <stdio.h>

int main(int argc, char **argv) {
    printf("Hello world\n");
    return 0;
}
bash
# 直接编译 C
zig build-exe hello.c
ldd hello
file hello
# 静态链接
# x86_64-macos
# x86_64-macos-musl 失败
zig build-exe hello.c --library c -target x86_64-macos-gnu
# macOS 下交叉编译 - 静态链接 ~11k
zig build-exe hello.c --library c -target x86_64-linux-musl
file hello
  • 交叉编译第一次会慢 - 预编译一些内容

关联信息

反向链接、本文链接的其他页面和外部资料。

参考资料

GitHub4 条
其他外链5 条
最近更新commit 9483fed编辑