How to emit headers file from build.zig. zig 0.14.0

I’m want to write shared library to try zig in a new case. There’re nothing easier then building shared library, but I can’t figure out, how to emit headers in build.zig. While I’m calling lib.getEmittedH, it’s just crushing the build…
Also, haven’t found any information about emiting C-like structs.
I use zig 0.14, and 0.13 gave the same result.

I always have an option to write header myself, but I think creating proper headers from zig build would be much better solution.

P.S I’m testing building with the example library that being created by zig init.

1 Like

Emitting headers is still broken.
See: How to enable build.zig also emit header file? - #6 by castholm

I’m using this code:

...
    const lib = b.addSharedLibrary(.{
        .name = "breakdown",
        .root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
    });

    const installed_lib = b.addInstallArtifact(lib, .{});
    installed_lib.emitted_h = lib.getEmittedH();
    b.getInstallStep().dependOn(&installed_lib.step);
...

It just throws
error: the following build command failed with exit code 1

May be -femit-h have been broken again. Because calling zig build-lib -femit-h .\src\root.zig in the shell produces no files.

Also, tried to change to the zig version to 0.13.0, and there no files were emitted.

any progress? i can’t figure this out either

I’ve found nothing…
I’ve written header file by hand for my task. It’s not a solution, but in this case I think it’s faster to do it by hand, then trying to fix.
Since even -femit-h doesn’t work (I didn’t managed to make it work), I think in current release it’s not possible…

Edit: If you will manage to fix it for your build, I’d like to see that. May be I’m doing something wrong…

1 Like

emit-h is broken. It’s Zig, not you.

2 Likes

Hi.
Is there any information on when this will be fixed?

Currently, there are an issue opened on github.
It’s assigned to 0.15.0 milestone, but it can be reassigned to later versions, as it’s not priority feature (from my understanding).

Damn, I wanted to use Zig as a bridge between C libraries and Rust, but it’s getting in the way.
I’ll have to write the .h file manually so Rust’s bindgen can consume it.

1 Like

Just voicing my support for prioritizing this functionality.

Writing the header by isn’t the worst problem in the world for some small wrappers, but it is slightly disappointing to not have it available. That’s not intended to be a dig or a slight, either – I totally understand that this is the risk of using an in-development language. But, hoping some progress can be made on this! Writing Zig code to interface directly with other C/C++ libraries is a really powerful and compelling use case.

1 Like

There was an effort by @SuperAuguste to revive it, but it wasn’t merged: Bring emit-h back to life! by SuperAuguste · Pull Request #20353 · ziglang/zig · GitHub

The compiler is indeed telling me that this feature is broken:

λ zig build 
install
└─ install mylib
   └─ compile lib mylib Debug native failure
error: error: -femit-h is currently broken, see https://github.com/ziglang/zig/issues/9698

I’d like to give a +1 on this feature.
IMO, the argument “if you develop it in zig, you can use it in C & C++” is pretty impactful to convince people on adopting the language !

1 Like