-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resurrect emit-h #9698
Comments
All struct types are unique, you cannot mix the automatically translated struct types with handwritten definitions. |
This comment was marked as duplicate.
This comment was marked as duplicate.
Wow using emails with GitHub is awful, this is the original message:
|
Either that or add casts where needed.
Unless there is a dependency loop, this should be pretty easy to do by chaining a |
Wouldn't the translated struct from emitted header still be different from the original one? I don't see how that will make the |
I agree. I'm inclined to think we'll have different structs in play even when headers can be emitted. For example when writing a combination of Zig and C code where you wish to share a Zig defined structure:
You couldn't even use cImport with cInclude of the exported header back in Zig because the header file would not exist initially (or be out of date). Not that you would want to use imported versions of your own packed struct in the Zig code. So, outside of using void* and casting either side, what is the ultimate solution here? |
You should define your structs in only one place. If you define them on the C side, you can cImport that in exactly one place, and everything will work. If you define them on the Zig side, you will also need to write your own extern function definitions, or post-process the generated .zig file from translate-c to fix up references. |
Agreed, but this doesn't account for emitted structs. The only reason the struct was hand-written in the example was because emit-h has been disabled for stage 1 since 0.7. I think that seamless interoperation with C is one of the goals of Zig, and getting structures defined in Zig to work in C should be a supported use case, as it's crucial for writing C ABI libraries in Zig. |
Completely agreed. The plan is to resurrect the emit-h feature in the self-hosted compiler, which is under active development. |
This branch was not intended to introduce new test coverage on the emit-h feature. See ziglang#9698
This branch was not intended to introduce new test coverage on the emit-h feature. See ziglang#9698
The "Add a Zig compilation unit to C/C++ projects" point in "⚡ Maintain it with Zig" section of main advertised language features should be removed until the header generation is back in a working state |
@Renha, why? That feature works entirely as described - header generation is a completely separate issue. |
So you could add Zig unit to the project, but not use it, and that was the intention? |
@Renha lack of emit-h doesn't make Zig compilation units unusable. |
Thank you, sorry if my tone sounded too harsh, I hope things will improve soon so that I would be able to use Zig in my project. |
As a workaround, you can usually get good results by asking an LLM to generate C headers from the Zig code. They are good at converting between formats. |
@abhinav Do you know of any public examples of handwritten headers for zig libraries that I could look at? |
@saltzm Yeah, sure! Off the top of my head:
If I had to guess, most projects using Zig 0.11 or newer, and exposing a C API would be hand-writing them right now. |
Thanks for the quick response! I'm looking at the first one. So basically it looks like the functions in the header you linked correspond to the exported functions from c_api.zig, and then in the header you linked they're defining some opaque structs where they manually determine the size of each struct in order to allow the user to statically allocate these structs. The latter bit is something I would want to do since I don't want my struct to be required to be heap allocated, but it definitely seems a bit difficult/convoluted. I can think of a workaround that would be providing an allocator to a Feel like I'm getting off topic here though, I think I get the gist now so thank you! Currently discussing this in the Zig Discord channel on zig-help by the way. EDIT: Ah, I see that the Tigerbeetle people are just redefining the struct in C to avoid the opaque struct stuff the libxev person is doing. |
As of
, but got this: EmittedH_0.13.0-dev.344+b2588de6c.log. Is it a bug? |
I am converting a codebase from C++ to Zig. So far, I switched the entry point to Zig and recreate the C++ objects in Zig. The rest in the middle are still (extern "C") C++, i.e. we have a Zig sandwich here.
In order for the C++ code to use the Zig struct, there must be a header file and unfortunately it is also translated to Zig. Thus, this shared module is recognized both as originally declared and
.cimport[…].struct_[…]
and the (stage 1) compiler yields theerror: expected type […], found […]
. I made a minimal reproducer here hoping it'll help debugging.I also noticed that stage 1 compiler has disabled C header emission (GH-6753), was this one of the reasons?
The text was updated successfully, but these errors were encountered: