Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Nice. Is zig easily callable with the C ABI? This could be a really nice thing to have for everybody using a different language too


In Zig you literally just import a C header and start coding. There's no FFI like rust and no header guards like C++.


By the way, Vlang has that level of C interop as well (https://github.com/vlang/v/blob/master/doc/docs.md), plus an outright C2V transpiler (https://github.com/vlang/c2v) and can be compiled to human-readable C.


The question wasn't if you can call C from Zig code, but if you can call Zig from C code.


You can, but you must explicitly declare your function wants to use C calling convention. See https://ziglang.org/documentation/master/#toc-export for an example, looks like this:

    fn internalName() callconv(.C) void {}
It will not compile if you use types that the C-calling convention does not allow. Without this, no, you can't call Zig from C.


My mistake


Zig and C are best friends. Zig can even compile C.


IIRC there were some limitations with C macros, if that is a think, my understanding of C is very limited, but I was interested in zig a while ago and watched some presentations.


Yes not everything can be imported perfectly. With my project making a Lua wrapper library in Zig I had to fix a few poorly translated macros. Overall though most translated just fine.

I think with some work on Zig’s end a few more cases could be handled though.


Yea :) In fact I'm planning to add a C ABI to Capy (I already did some small tests in the c_exemples folder)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: