Help add c include path

Hi all!
Very new to ZiG,
trying to import c header that is located in standard /usr/include, but doing something like

const stuff = @cImport({
    @cInclude("mystuff.h");
});

yields error: 'mystuff.h' file not found

i’ve googled around and found a simple solution - to add to build.zig something like exe.addIncludeDir("/usr/include");
while it itself looks like a hack - I cannot even find addIncludeDir function in zig 0.11, and cannot find any release notes about it…

can please someone help me with this tiny thing?

Hi @hex, welcome to ziggit.

The function is called addIncludePath
Usage is: exe.addIncludePath(b.path("/path/to/include"));

2 Likes

Oh thank you!
By the way, is this the best way to do it? Sounds like including standard locations like /usr/include may be done with some special flag/option/function, no?

1 Like

I don’t know. I am guessing, that zig does not include the system includes because it bundles c libraries like musl, gnu and mingw.

In Zig 0.12.0, you have to now add the include path for @cInclude() to the Zig module in question (using std.Build.Module.addIncludePath). std.Build.Step.Compile.addIncludePath appears to be applicable only to C files added using addCSourceFile(s).

Just in case anyone runs into this. I was banging my head wondering why the compiler just isn’t finding the stupid file :stuck_out_tongue:

1 Like

That does not seem to work anymore. The build system will complain about e.g. /usr/include being an absolute path and then continue to crash; you can use something like ../../../../../usr/include, but that kinda feels stupid.

But at least it works.

The error message printed when you use b.path("/usr/include") tells you what to do:

sub_path is expected to be relative to the build root, but was this absolute path: ‘/usr/include’. It is best avoid absolute paths, but if you must, it is supported by LazyPath.cwd_relative

In other words, replace it with std.Build.LazyPath{ .cwd_relative = "/usr/include" }, like so:

-exe.root_module.addIncludePath(b.path("/usr/include"));
+exe.root_module.addIncludePath(.{ .cwd_relative = "/usr/include" });

b.path() is only meant for subpaths of the build root (the directory containing your build.zig file). I’m surprised it even allows b.path("../../../../../usr/include"), this seems like a bug.

1 Like

Thanks!

The error message printed when you use b.path("/usr/include") tells you what to do:

I am new to zig, so I did not understand that message. That is why I asked here.