captain_error
captain_error

Reputation: 21

How to make the Zig build system emit a c-header along with a generated library

Calling zig build-lib -femit-h my_lib_source.zig, a c-header file can be exported for a library written in zig.

How can the same be achieved in build.zig?

Could this be achieved with an option to b.installArtifact?

I found no documentation about this at https://ziglearn.org/chapter-3/.

Upvotes: 2

Views: 3007

Answers (2)

Dave Lancea
Dave Lancea

Reputation: 1689

I looked into this a bit and I believe getEmittedH is currently broke. Maybe it works if you're also compiling an exe from a .c file (like the official documentation shows) but compiling a zig library by itself getEmittedH() puts the .h in the wrong directory (see this GitHub issue: https://github.com/ziglang/zig/issues/18497).

Adding

_ = lib.getEmittedH();

after

   b.installArtifact(lib);

does create the .h, but it's in the root of zig-cache.

Upvotes: 1

Der_Teufel
Der_Teufel

Reputation: 21

Assuming you've created a lib compile step as const lib = b.addStaticLibrary(...) you can write

const install_header = b.addInstallFile(lib.getEmittedH(), "include");
b.getInstallStep().dependOn(install_header);

which should install the generated header file into a folder called include inside zig-out

Upvotes: 0

Related Questions