Tomek
March 24, 2023, 5:20am
1
I’m looking for some examples (docs?) about using structures defined in Zig in C code. In particular: having defined a structure in Zig:
how can it be passed to C function?
how can it be handled in this C function?
Returning such a structure from C function can be handled by returning the passed structure, so it shouldn’t be a problem, I suppose.
nurpax
March 24, 2023, 11:25am
2
I found this a little unclear in the docs so I switched it around: I declare C structs in C code for anything that needs to be accessed in C functions. Then I import those into Zig and fill in there. I use “translate-c” for converting the .h interface files into Zig although @cImport
is supposed to work for that too.
Below is the error that I get. FWIW, using #include <header> is not ideal, it should be #include "header" but seems to be not what Zig’s doing.
But I was able to fix it too. In my case, I have split my project into “utils” (lib) package and “main” (exe). I had added the include path for “utils” with lib.addIncludePath("src/gfxutil/"). With just this, building main exe fails as below. But if I also add exe.addIncludePath("src/gfxutil/") to the exe build options, it works. This is a little …
Tomek
March 25, 2023, 6:00pm
3
Thx for that suggestion. Should work for situations, when Zig structures don’t need to be accessed in C code.