简体   繁体   中英

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

I've recently discovered zig and find it very interesting. I'm now trying to learn how to use zig as a cross compiler and, the following builds and runs fine (on Windows)

zig cc -Wno-everything src/ctest.c

however, when I use the build-exe command or build script, the (Windows) libc cannot be found and 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")
...

If I could see what zig cc really does, maybe I could figure it out (but zig cc does not seem to allow the --verbose-cc flag). Or how can I get zig to link with msvc (or any other working libc) on Windows? For completeness, the build.zig script is effectively:

...
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);
...

Here's the relevant issue for this: https://github.com/ziglang/zig/issues/514

Once the Windows libc portion of this issue is implemented, your example will work. Until then, cross compiling for Windows code that wants to link libc would need a cross compiling environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM