What is the ideomatic way to import a basic C library?

Hello!

I’ve been trying to add microui as a dependency for my project. I initially tried to zig fetch --save … as I normally do, but because it’s missing a build.zig.zon file in it’s root that doesn’t work. I can think of a few ways to go about this but they all feel wrong:

  • fork it and add a build.zig.zon file, fetch the fork instead
    • feels the most “correct” but means I’m left maintaining it somehow. That’s not as hard as I’m making it out to be but it’s still uncomfortable
  • fetch without --save at build time (like discussed here)
    • easily takes the most work of any of these options, and only seems to be worth doing in very odd circumstances
  • add it as a git submodule
    • I’d do this for a C/C++ project, but only as a “least bad” option. It’s worse here because deps would be split between build.zig.zon and .gitmodules
  • add it to a vendor directory
    • probably what I’ll do in the absence of any advice because it’s the easiest, but this incurs some of the maintenance issues of forking while also throwing out any tooling to handle dependencies
  • make it a system dependency
    • means that others (or future me) will have to go through extra steps for zig build to work

This feels like it should be pretty easy considering zig/c interop has generally felt nice, so I’m mildly embarrassed that I can’t figure this out.

zig fetch --save=microui will add the tarball to build.zig.zon with the name microui.

build.zig.zon (if you crack it open) is designed to be human-editable, and zig build will correct you if you miss, for example, the checksum.

2 Likes

For dependencies without a build.zig.zon file, you can use zig fetch --save=name ... to grab the dependency. So zig fetch --save=microui ...

1 Like

I was already aware that build.zig.zon is human-editable. I was just under the impression, through a series of misreadings, that the build system doesn’t know what to do at all with a zip not containing it, and figured that --name=... was to prevent two dependencies of the same name causing conflicts. Thanks though!

1 Like

it is, well its more general than that. It provides the name call it in build.zig.zon and buid.zig.

Prevent conflicting names is one use. Providing a name for things that don’t have a build.zig.zon (that’s where zig fetch gets the name from) like a c dependency is another use.

1 Like