Create binaryen bindings library

Hey everyone.
I’m using cmake to build binaryen and create a a file. And I want to link it with Zig library to build a nicer interface.

  • I successfully linked binaryen to an exe (see the repo).
  • I’m unable to understand how to create a static library with binaryen in it.
  • Are there any guides I can read about Zig build system?

Disclaimer
I’m very new to zig and c.

Repo: GitHub - Tzelon/binaryenzig

Thank you.

Hello @Tzelon Welcome to ziggit :slight_smile:

There is the official: Zig Build System
and the ziggit documentation page: Build system tricks

see:

1 Like

Hey @dimdin, thank you for the links.
I found out what was the problem. it was an issue in v0.12.0

Upgrading to v0.13.0-* fixed it.

I’m able to build the lib, but I have a few questions
** I took inspiration from zig-cli/build.zig at main · sam701/zig-cli · GitHub

  1. my understanding of modules is that they are zig “packages” I want to import into my project.
    using addModule will add a named module to be used in @import .
  2. what is the root_module I use in simple.root_module.addImport?
  3. what is the use of staticLibrary in this case?
  4. why do I need to use linkSystemLibrary? Is it not enough to give the include path and library path?

Here is the new build file

const std = @import("std");

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

    const module = b.addModule("binaryenzig", .{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .link_libcpp = true });

    module.addIncludePath(b.path("binaryen/src/"));
    module.addLibraryPath(b.path("binaryen/lib/"));
    // lib.addObjectFile(b.path("binaryen/lib/libbinaryen.a"));
    module.linkSystemLibrary("binaryen", .{});

    // module.linkLibCpp();

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

    // lib.installHeadersDirectory(b.path("binaryen/src/"), "binaryen", .{});
    lib.addIncludePath(b.path("binaryen/src/"));
    lib.addLibraryPath(b.path("binaryen/lib/"));
    // lib.addObjectFile(b.path("binaryen/lib/libbinaryen.a"));
    lib.linkSystemLibrary("binaryen");

    lib.linkLibCpp();

    b.installArtifact(lib);

    const simple = b.addExecutable(.{
        .target = target,
        .name = "simple",
        .root_source_file = b.path("examples/simple.zig"),
        .optimize = optimize,
    });
    simple.root_module.addImport("binaryenzig", module);
    b.installArtifact(simple);
    b.default_step.dependOn(&simple.step);
}

1 Like