Search code examples
zig

Fatal error: 'stdio.h' file not found in compiling c file


I downloaded libwsclient c library as git clone https://github.com/payden/libwsclient.git Inside the generated folder libwsclient I wrote build.zig as:

const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
    const target = b.standardTargetOptions(.{});
    const mode = b.standardOptimizeOption(.{});
    const lib = b.addStaticLibrary(.{
        .name = "wsclientlib",
        .root_source_file = .{ .path = "wsclient.c" },
        .target = target,
        .optimize = mode,
    });
    b.installArtifact(lib);
}

And tried to build the library as zig build but I got the below:

[hasany@hasan-20tes1n300 libwsclient]$ zig build
zig build-lib wsclientlib Debug native: error: error(compilation): clang failed with stderr: /home/hasany/Documents/zig-tutorial/chrome-development-tool/libwsclient/wsclient.c:1:10: fatal error: 'stdio.h' file not found

zig build-lib wsclientlib Debug native: error: the following command failed with 1 compilation errors:
/usr/bin/zig build-lib /home/hasany/Documents/zig-tutorial/chrome-development-tool/libwsclient/wsclient.c --cache-dir /home/hasany/Documents/zig-tutorial/chrome-development-tool/libwsclient/zig-cache --global-cache-dir /home/hasany/.cache/zig --name wsclientlib -static --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install wsclientlib transitive failure
   └─ zig build-lib wsclientlib Debug native 1 errors
/home/hasany/Documents/zig-tutorial/chrome-development-tool/libwsclient/wsclient.c:1:1: error: unable to build C object: clang exited with code 1

Solution

  • fatal error: 'stdio.h' file not found

    This is a hint that you need to tell Zig to link libc.

    You can do this in build.zig by adding

    lib.linkLibC();
    

    This is equivalent to adding the -lc argument to the zig build-lib command.