Wasm build error due to posix.system__struct_1539

Hi here! I am currently learning zig, and I have a lot of joy on using it since it has rust like syntax which I kinda like but without all the hurdle of rust. After gone though the ziglings exercises and opened a project just to mess around the basic built-in data structures, the allocators and some patterns for dynamically creating some structs on heap, I decided to have a look to web assembly since I have a plan on rewriting some of my older projects in zig as a web app.

However, I have faced a build error, but neither I can understand the meaning of the error because it is deep into the standard library, nor I can find any similar problems anywhere. Let me show you the code for the details:

My application is simple, just a simple debug print in src/main.zig:

const std = @import("std");

pub fn main() !void {
    std.debug.print("Hello World", .{});
}

Meanwhile, here is my build.zig file:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.resolveTargetQuery(.{
        .cpu_arch = .wasm32,
        .os_tag = .freestanding,
        .abi = .none,
    });

    // Expose '-Doptimize=[enum]' as a command-line option.
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "main",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    exe.entry = .disabled;
    exe.rdynamic = true;

    b.installArtifact(exe);
}

When I build the file directly using the following command:
zig build-exe src/main.zig -target wasm32-wasi

It works perfectly fine, and I can confirm the program works by running wasmtime.

However, sometimes, I wanna use some dependencies for the build, so I want to use the build file to build the project, but when I build the project using the command zig build, I got the following error:

...\zig-windows-x86_64-0.13.0\lib\std\posix.zig:121:24: error: struct 'posix.system__struct_1539' has no member named 'fd_t'    
pub const fd_t = system.fd_t;

Anything I have done wrong or misunderstood with the build.zig file?
Let me know if anyone want more information for the issue.

There you use wasi, but in your build you use freestanding, have you tried using wasi in your build.zig too?

As far as I understand freestanding is basically “nothing”/no-OS and expects you to implement everything yourself, including a lot of basic things like printing things and it is usually used to get full control over things and implement things like kernels. But I am far from being an expert, I haven’t really touched freestanding.

I think for more comfortable wasm32 usage, you would either want to use wasi (run wasm outside of a browser) or emscripten, emscripten requires some extra steps but allows you to run it in a browser.

Welcome to ziggit!

1 Like

Thanks for your reply! Seems changing the .os_tag from .freestanding to .wasi works, and run without issue. Although it is a bit complicated at first glance, I will have a look to the emscripten approach because that is the objective of my web app.

Edit: your provided example also included raylib, which is perfect!

1 Like