Zig build fails to compile C program when using "-Werror" flag and optimized for safety

Here’s the build file. I know that I can remove the “-Werror” when compiling, but I just wanted to bring this up.

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize: std.builtin.OptimizeMode = .ReleaseSafe;

    const exe_mod = b.createModule(.{
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });

    exe_mod.addCSourceFile(.{
        .file = b.path("src/main.c"),
        .flags = &.{"-Werror"},
    });

    const exe = b.addExecutable(.{
        .name = "test",
        .root_module = exe_mod,
    });

    b.installArtifact(exe);
}

zig version 0.14.0. I’m sorry if this is already fixed.

could you provide the build error(s) you’re getting

I’m sorry, my bad.

install
└─ install test
   └─ zig build-exe test ReleaseSafe native 1 errors
:1:1: error: argument unused during compilation: '-fno-rtlib-defaultlib'
error: warning(compilation): failed to delete 'C:\<file path>\.zig-cache\tmp\e4c52f5133ab6b2e-main.obj.d': FileNotFound

error: the following command failed with 1 compilation errors:
C:\apps\zig\zig.exe build-exe -cflags -Werror -- C:\<file path>\src\main.c -OReleaseSafe -Mroot -lc --cache-dir C:\<file path>\.zig-cache --global-cache-dir C:\Users\behon\AppData\Local\zig --name test --zig-lib-dir C:\apps\zig\lib\ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install test transitive failure
   └─ zig build-exe test ReleaseSafe native 1 errors
error: the following build command failed with exit code 1:
C:\<file path>\.zig-cache\o\b6fa6748e49a29bb4c698633daa1f433\build.exe C:\apps\zig\zig.exe C:\apps\zig\lib C:\<file path> C:\<file path>\.zig-cache C:\Users\behon\AppData\Local\zig --seed 0x4038a9e1 -Ze8c237d7774b6655

first error looks like zig adding a flag that isnt needed, though it might not be able to accurately determine if its needed so might be a bug, might not.

seccond error: it looks like your building from your editor and some placeholder didnt get replaced

1 Like

Yeah the error related to the -fno-rtlib-defaultlib flag is a bug on the llvm side I think.

I’m not sure what you meant by “some placeholder didn’t get replaced”. Can you elaborate a bit? I built it from the command prompt.

It is fixed on the master and 0.14.1 branch.

1 Like