Hey,
I’m very new to zig so I’m a bit lost on how to actually include any c header into my code. The documentation that I’ve found is also quite sparse.
Right now I have a c header file in a seperate directory called “external”.
To my understanding to include the file in my main.zig file I have to do the following:
const c = @cImport(@cInclude("external/stb_image.h"))
but I’m getting an “error: 'external/stb_image.h' file not found”. Do I have to include that directory and file somehow in the build.zig file?
You need to add an include path so Zig knows where to find that header. exe.addIncludePath() is what you’re looking for if you’re using build.zig.
Also, if you want to use an external dependency, instead of managing it yourself, you can have zig automatically download it/cache it by putting it in your build.zig.zon file, i.e.
assuming you used zig init your main.zig file will be in src/main.zig, so I would guess that external is a sibling directory to src, if that is the case you would need this:
const c = @cImport(@cInclude("../external/stb_image.h"));
Alternatively you could use:
exe.addIncludePath(b.path("external"))
within your build.zig and then use:
const c = @cImport(@cInclude("stb_image.h"));
in your code or even better do what @marler8997 suggested to add the dependency to your project.
However stb_image needs a define set, otherwise you won’t have the implementation part of the library in your build.
I think you could add the implementation part separately from the cImport/cTranslate to get the best compatibility (only put the header part through translate and the implementation part through addCSourceFile), I think you can do that by adding this to your build.zig (may need testing/some small changes):
Thanks guys for the swift answer, sorry for the late reply tho. Just wanted to let you know that the solution worked thank you!
Just so that I understand it better the addWriteFiles() allows me to add a file to the build which references the stb_image.h header correct?
Which it can find since we’ve added the include path “external”.
I’ve read that @cImport might be removed in future versions of Zig so you might have to import the C header file as a module in your build file and then @import the module in your source file normally. For instance: