Search code examples
webassemblyzig

How do I specifiy the "-rdynamic" compiler/linker option in a zig.build file?


I'm using zig 0.10.1 and I'm trying to build a wasm library. Most tutorials show how to do this with zig build-lib. For a wasm library to export symbols one needs to specify the -rdynamic option. Now that I'm trying to build the library with a zig.build file, I can't pass -rdynamic as a compiler option anymore. Therefore the created library does not export my "exported functions" anymore.

First I tried zig build -rdynamic but it says it does not recognize this option and zig build -h doesn't seem to show any such option.

I also tried to look at the source of the builder passed to the build function in zig.build but the source for this std.build.Builder doesn't exist (no Builder.zig file): https://github.com/ziglang/zig/tree/0.10.x/lib/std/build Am I looking in the wrong place?

Any help would be appreciated!

Here is my simple build file:

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

pub fn build(b: *Builder) !void {
    // Standard target options allows the person running `zig build` to choose
    // what target to build for. Here we do not override the defaults, which
    // means any target is allowed, and the default is native. Other options
    // for restricting supported target set are available.
    const target = b.standardTargetOptions(.{});

    // Standard optimization options allow the person running `zig build` to select
    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
    // set a preferred release mode, allowing the user to decide how to optimize.
    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addSharedLibrary(.{
        .name = "math",
        .root_source_file = .{ .path = "math.zig" },
        .target = target,
        .optimize = optimize,
    });
    b.installArtifact(lib);
}

This is math.zig:

extern "env" fn print(i32) void;

export fn add(a: i32, b: i32) void {
    print(a + b);
}

Loading the generated wasm file with this WebAssembly Code Explorer indeed shows that add() was not exported.


Solution

  • lib.rdynamic = true;

    Really ought to be in SharedLibraryOptions but it's not at the moment.