Skip to content
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

C API #65

Open
VarLad opened this issue Jan 22, 2024 · 11 comments
Open

C API #65

VarLad opened this issue Jan 22, 2024 · 11 comments

Comments

@VarLad
Copy link

VarLad commented Jan 22, 2024

I would love to use this in projects outside Zig as well.
Any idea if this project can provide a C API?

@david-vanderson
Copy link
Owner

Good idea! I haven't thought about this before. Just to make sure, are you thinking to compile dvui into a static library and call the functions from C?

I think this should work with some modifications (adding extern to various functions). Give me some time and I'll try to make a prototype example.

@VarLad
Copy link
Author

VarLad commented Jan 22, 2024

@david-vanderson Yeah, exactly that
Ideally the project could provide a C header file one can easily use.

@iacore
Copy link
Collaborator

iacore commented Jan 22, 2024

@VarLad you might be better off using imgui with C. Zig has types like ?T that is incompatible with C ABI.

@david-vanderson
Copy link
Owner

I also recommend dear imgui, but I suspect you already know about it. In any case it will still be useful to see if exposing a C API can be done.

@iacore
Copy link
Collaborator

iacore commented Jan 22, 2024

It can be done but it is difficult.

Every error.X need a fixed number to map to it.
Every enum variant need a fixed number to map to it.
union(enum): not exist in C.
?T: not exist except optional pointer.

@david-vanderson
Copy link
Owner

I spent some time digging into this. It seems like this should be possible, but is currently not well supported.

ziglang/zig#9698 looks like the tracking issue for this in zig. I was able to use zig build-lib -femit-h=test.h test.zig to autogenerate a header for exported functions, but then ran into ziglang/zig#14381. All my attempts via build.zig also failed, sometimes with an error like error: unable to create compilation: NoZigModuleForCHeader

And this is all before getting into the issues that @iacore listed.

@VarLad It looks like this isn't the right time to attempt this, do you agree? Maybe we should revisit this after zig 0.12 is released and see if things have gotten better.

@VarLad
Copy link
Author

VarLad commented Jan 23, 2024

I agree with that. We can revisit the issue after Zig 0.12 release.

@Renha
Copy link

Renha commented Jan 27, 2024

The tracking issue has 0.13.0 milestone set 😭

@david-vanderson
Copy link
Owner

Good point. If 0.12 doesn't fix it, then we might have to try doing it "manually" - using -femit-h but hand editing the output.

@VisenDev
Copy link
Collaborator

I did a little bit of testing related to this in C. It does appear to be possible to recreate zig's @src() in C (which is something I thought might be a problem for using dvui with C)

#include <stdio.h>
#define SRC {__FILE__, __LINE__}

typedef struct {
    char*file;
    int line;
} Source;

int main() {
    Source a = SRC;
    printf("%s%i", a.file, a.line);
}

@david-vanderson
Copy link
Owner

That is very interesting - well done!

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

No branches or pull requests

5 participants