使用 Zig 编译器构建 C 代码时如何包含 (msvc) libc

How to include (msvc) libc when building c code with the Zig compiler

我最近发现了 zig,觉得它很有趣。我现在正在尝试学习如何将 zig 用作交叉编译器,并且以下构建和运行良好(在 Windows)

zig cc -Wno-everything src/ctest.c

但是,当我使用 build-exe 命令或构建脚本时,无法找到 (Windows) libc 并且 linked

c:\zigctest>zig build

Zig is unable to provide a libc for the chosen target 'x86_64-unknown-windows-msvc'.
The target is non-native, so Zig also cannot use the native libc installation.
Choose a target which has a libc available, or provide a libc installation text file.
See `zig libc --help` for more details.
The following command exited with error code 1:
c:\zigctest\zig.exe build-exe --library c --c-source -Wno-everything C:\zigctest\src\ctest.c --cache-dir C:\zigctest\zig-cache --name ctest -target x86_64-windows-msvc --cache on
exec failed
C:\zigctest\lib\zig\std\build.zig:768:36: 0x7ff76fece654 in std.build.Builder::std.build.Builder.exec (build.obj)
                    std.debug.panic("exec failed")
...

如果我能看到 zig cc 的真正作用,也许我能弄明白(但 zig cc 似乎不允许 --verbose-cc 标志)。或者如何在 Windows 上使用 msvc(或任何其他可用的 libc)将 zig 连接到 link?为了完整起见,build.zig 脚本实际上是:

...
const cflags = [][]const u8{
"-Wno-everything",
};

const exe = b.addExecutable("ctest", null);
exe.linkSystemLibrary("c");
exe.setBuildMode(mode);
exe.setTarget(builtin.Arch.x86_64, .windows, .msvc);
exe.addCSourceFile("src/ctest.c",cflags);
...

这是与此相关的问题:https://github.com/ziglang/zig/issues/514

一旦实现了这个问题的 Windows libc 部分,您的示例就会起作用。在那之前,交叉编译想要 link libc 的 Windows 代码需要一个交叉编译环境。