Search code examples
zig

Failed to define the target at build.zig


I'm at linux and want to cross compile for windows, I've the below that is working file for building the executable at linux:

pub fn build(b: *std.build.Builder) void {

    const target = b.standardTargetOptions(.{});

    // const target = "x86_64-windows";


    const mode = b.standardReleaseOptions();

    const exe = b.addExecutable("Zig", "src/main.zig");
    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.install();
}

I tried to use const target = "x86_64-windows"; but it failed telling that error: expected type 'zig.CrossTarget', found '*const [14:0]u8'


Solution

  • you need to pass a CrossTarget.

    const target = std.zig.CrossTarget{ .os_tag = .windows, .cpu_arch = .x86_64 };
    

    also as @ad-absurdum said you can just run zig build -Dtarget=x86_64-windows