How to use raylib as a dependency of a dependency?

The described approach is working for me to link raylib to my actual zig project and use it within build an executable:

Now I try to build a static library from my zig project that depends on raylib, and export it also as a module (firefly). I used the following as a starting point:

My relevant build file code looks like:

const raylib_dep = b.dependency("raylib", .{
        .target = target,
        .optimize = optimize,
    });

    const firefly = b.addStaticLibrary(.{
        .name = "firefly",
        .root_source_file = .{ .path = "src/lib.zig" },
        .target = target,
        .optimize = optimize,
    });

    firefly.installHeadersDirectory(raylib_dep.path("src/"), "", .{
        .include_extensions = &.{".h"},
    });
    firefly.linkLibrary(raylib_dep.artifact("raylib"));
    firefly.linkLibC();
    b.installArtifact(firefly);

    const firefly_module = b.addModule("firefly", .{
        .root_source_file = .{ .path = "src/lib.zig" },
    });
    firefly_module.addIncludePath(raylib_dep.path("src/"));

Then in another project I do like described within the article above, add the firefly dependency and try to link the firefly module like:

const firefly_dep = b.dependency("firefly", .{
        .target = target,
        //  .optimize = optimize,
    });
    const firefly_module = firefly_dep.module("firefly");

    const exe = b.addExecutable(.{
        .name = "firefly-zig-example",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    exe.root_module.addImport("firefly", firefly_module);
    exe.linkLibrary(firefly_dep.artifact("firefly"));

On build I always get the following error:

C:\Users\anhef\AppData\Local\zig\p\12201eeeb3ea209ca3061915e81c73cb1f0257870884a6959927a9c052a2c1341dd1\src\inari\firefly\api\raylib\rendering.zig:3:12: error: C import failed
const rl = @cImport(@cInclude("raylib.h"));
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    active_clear_color: C:\Users\anhef\AppData\Local\zig\p\12201eeeb3ea209ca3061915e81c73cb1f0257870884a6959927a9c052a2c1341dd1\src\inari\firefly\api\raylib\rendering.zig:168:30
    initImpl: C:\Users\anhef\AppData\Local\zig\p\12201eeeb3ea209ca3061915e81c73cb1f0257870884a6959927a9c052a2c1341dd1\src\inari\firefly\api\raylib\rendering.zig:77:9
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
C:\Users\anhef\dev\zig-dev\firefly-zig-example\zig-cache\o\a7286b5c9fe6d89c1a5ce9384c6db9aa\cimport.h:1:10: error: 'raylib.h' file not found
#include <raylib.h>
         ^

I tried to play around with the paths but with no success. The raylib.h and other headerfiles seems to be present in the cache but are not found?

Thanks for any help.

I got this working now by including the raylib dependency also within the example project that uses the firefly library (that uses raylib).

The relevant parts in my “firefly” library project build files look now as follows:

firefly/build.zig.zon:

.{
    .name = "firefly",
    .version = "0.1.0",

    .dependencies = .{
        .raylib = .{
            .url = "https://github.com/raysan5/raylib/archive/f15455552da353dfb7055b6fa020ba73d71de88e.tar.gz",
            .hash = "1220f4c20b801db653e9938cca7e39273f1894e68192f555e648296112358cef9046",
        },
    },
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "README.md",
        "src",
    },
}

firefly/build.zig:

...
    // define the raylib dependency 
    const raylib_dep = b.dependency("raylib", .{
        .target = target,
        .optimize = optimize,
    });

    // create firefly library
    const firefly = b.addStaticLibrary(.{
        .name = "firefly",
        .root_source_file = .{ .path = "src/lib.zig" },
        .target = target,
        .optimize = optimize,
    });

    b.installArtifact(firefly);

    // expose firefly library as a module
    const firefly_module = b.addModule("firefly", .{
        .root_source_file = .{ .path = "src/lib.zig" },
    });
    firefly_module.addIncludePath(raylib_dep.path("src/"));

    // build executable for examples
    const exe = b.addExecutable(.{
        .name = "firefly-zig",
        // In this case the main source file is merely a path, however, in more
        // complicated build scripts, this could be a generated file.
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    exe.linkLibrary(raylib_dep.artifact("raylib"));
    b.installArtifact(exe);

...

Then in the other project where I want to use firefly as a dependency, I declare both, raylib and firefly as a dependency in the build.zig.zon file:

.{
    .name = "firefly-zig-example",
    .version = "0.0.1",

    .dependencies = .{
        .raylib = .{
            .url = "https://github.com/raysan5/raylib/archive/f15455552da353dfb7055b6fa020ba73d71de88e.tar.gz",
            .hash = "1220f4c20b801db653e9938cca7e39273f1894e68192f555e648296112358cef9046",
        },
        .firefly = .{
            .url = "https://github.com/AndreasHefti/firefly-zig/archive/bedf768fd44cdcafee5a5b115385f509695f5e3f.tar.gz",
            .hash = "12206f8f3f77d5570295340a1279c72a85a1f666f78634a2e9c5c0b35b1cd6ba19af",
        },
    },
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "README.md",
        "src",
    },
}

and in the build.zig file I install the firefly module with the raylib dependency like so:

// use raylib dependency
   const raylib_dep = b.dependency("raylib", .{
       .target = target,
       .optimize = optimize,
   });

   // use firefly dependency
   const firefly_dep = b.dependency("firefly", .{
       .target = target,
       .optimize = optimize,
   });

   // use firefly as module
   const firefly_module = firefly_dep.module("firefly");
   // we link the raylib dependency here to the firefly module
   firefly_module.linkLibrary(raylib_dep.artifact("raylib"));

   // build executable
   const exe = b.addExecutable(.{
       .name = "firefly-zig-example",
       .root_source_file = b.path("src/main.zig"),
       .target = target,
       .optimize = optimize,
   });

   exe.root_module.addImport("firefly", firefly_module);
   exe.linkLibrary(firefly_dep.artifact("firefly"));
   b.installArtifact(exe);

No you can import and use the firefly lib like:

const std = @import("std");
const inari = @import("firefly");
const firefly = inari.inari.firefly;

I still need to tweak the namespace of the firefly module and the “optimize” build attribute that seems not to be exposed. But generally it works this way with the additional raylib dependancy.