Skip to content

Add a Zig package for the C ABI library - #120

Draft
joshbainbridge wants to merge 1 commit into
c-abifrom
zig-bindings
Draft

joshbainbridge wants to merge 1 commit into
c-abifrom
zig-bindings

Conversation

@joshbainbridge

Copy link
Copy Markdown
Collaborator

Builds on #119. Lets Zig projects depend on OpenQMC with the Zig package manager, using the C ABI without hand-written bindings.

Overview

  • build.zig and build.zig.zon (Zig 0.17) build liboqmc as a static archive with hidden visibility. The sampler option chooses the sampler (default pmjbn).
  • oqmc_c.h is translated with the translate-c package (pinned to 2.0.2) into an oqmc module. Importing the module also links the library.
  • examples/zig is a separate project that depends on the package by path and calls every C ABI function, mirroring cmake/examples/cabi.
  • The flake adds zig and zls, and the Zig output directories are ignored and removed by just clean.
const openqmc = b.dependency("openqmc", .{
    .target = target,
    .optimize = optimize,
    .sampler = .pmjbn,
});
exe.root_module.addImport("oqmc", openqmc.module("oqmc"));

Testing

  • zig build run in examples/zig passes for all six samplers, in Debug and ReleaseFast.
  • Release archives depend only on libc (free, malloc, memcpy), and all their symbols are hidden.

To do

  • CI job for the Zig example.
  • README and changelog.

Zig projects can't depend on OpenQMC without CMake, and the C ABI has
no Zig bindings. Zig can translate the C header itself, so a package
that builds the library is enough for Zig to call the samplers, and
the same build can later produce the library for C and C++ users.

Add build.zig and build.zig.zon for Zig 0.17, building liboqmc as a
static archive with hidden visibility, the sampler chosen with the
sampler option. Translate oqmc_c.h with the translate-c package, pinned
to 2.0.2, into an oqmc module that links the library, and limit the
package paths to the files it needs. Add a separate example project in
examples/zig that depends on the package by path and calls every
function in the C ABI, mirroring the C example. Add zig and zls to the
flake, updating the locked nixpkgs revision, and ignore and clean the
Zig cache, output and package directories.

Signed-off-by: Josh Bainbridge <josh.bainbridge@gmail.com>
@joshbainbridge
joshbainbridge marked this pull request as draft November 17, 2026 00:52

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant