Include local C headers in project

I would like to work through Build your own lisp book using Zig and C combined just for learning Zig 0.13. However the following step requires me to download mpc.c and mpc.h to my Zig project and include them. So far I have been able to understand how to add the mpc.c file in build.zig like exe.addCSourceFile(.{.file = b.path("src/mpc.c") });. However when trying to include the mpc.h file i get errors that the file is not found.

const c = @cImport({
    @cInclude("editline/readline.h");
    @cInclude("mpc.h");
});

This works good for readline.h which is a system requirement but not for my local mpc.h file.

What am I missing, also in the link provided is says that mpc depends on the math lib so I will likely also have to depend on -lm when compiling.

Welcome to Ziggit @favetelinguis!

You can register the include file like this:

exe.addIncludePath(b.path("src/"));

assuming the mpc.h file is in the src/ directory.

You also might be able to use @cInclude("src/mpc.h"); directly instead (I am not entirely sure).

For -lm you can add:

exe.linkSystemLibrary2("m", .{});
3 Likes