Is it possible to inherit include paths and link libs from a module?


I have a module named ‘destral’ that is a collection of utility functions to handle SDL2 and other C libraries. Inside that module I call C SDL2 functions.

Then I want to create an executable that imports this module BUT is able to call SDL2 functions directly too. How can I instruct the module to export the include paths and linked libraries to the executable too?

This is what I tried, and it works except that It can’t find my SDL.h when compiling mygame.zig.

fn createExeCompileAndLinkStep(b: *std.Build, target: std.Build.ResolvedTarget, optimize: OptimizeMode, comptime exe_name: []const u8, source_path: []const u8) *std.Build.Step.Compile {
    const module = b.createModule(std.Build.Module.CreateOptions{
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/destral.zig"),
        .link_libc = true,
        .link_libcpp = true,

    linkSDL2(b, module, target);

    const exec_options = std.Build.ExecutableOptions{
        .name = exe_name,
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("mygame.zig"),
    const exe = b.addExecutable(exec_options);
    exe.root_module.addImport("destral", module);
    // IF I UNCOMMENT THIS, I CAN CALL SDL2 functions from my mygame.zig 
    //linkSDL2(b, &exe.root_module, target);
    // Is it possible to configure my module to expose this as a dependency?

    return exe;

fn linkSDL2(b: *std.Build, module: *std.Build.Module, target: std.Build.ResolvedTarget) void {
            module.linkSystemLibrary("Ws2_32", .{});
            module.linkSystemLibrary("wsock32", .{});
            module.linkSystemLibrary("winmm", .{});
            module.linkSystemLibrary("SDL2", .{});
            module.linkSystemLibrary("opengl32", .{});


With zig 0.11 this was working fine because the linked libraries and include paths were shared but this change in 0.12:

Changed that behavior and I don’t know how to fix it properly.

Thank you :slight_smile:

I don’t know if this helps, what you want to do is beyond my knowledge, but usingnamespace it might help … maybe.

Hello @RoiG Welcome to ziggit :slight_smile:

It is my understanding that systems libraries are inherited.
Note that the linker eliminates everything that is not used by your exe.

For calling SDL2 functions you can expose them from your library instead of including them again.

e.g. in you src/destral.zig you must have something like:

const sdl = @cimport ...

make it:

pub const sdl = @cimport ...

Then in your exe use it like:

const sdl = @import("destral").sdl;

Aaaaah it was so trivial,
Thank you @dimdin, it worked perfectly.

1 Like