How do I embed a file into the binary during build?

I’ve seen here that to embed a file I would need to add an anonymous import in the build.zig and use @embedFile. Here’s the relevant part of my build file:

    const exe = b.addExecutable(.{
        .name = "ness",
        .use_llvm = true,
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
            .strip = optimize == .ReleaseFast,
            .imports = &.{
                .{ .name = "ness", .module = mod },
            },
        }),
    });
    exe.linkLibrary(clay_lib);
    exe.linkLibrary(sdl_ttf_lib);
    exe.linkLibrary(glslang_lib);
    exe.linkLibrary(spirv_cross_lib);

    exe.root_module.addAnonymousImport("pixeloid_font", .{ .root_source_file = b.path("fonts/PixeloidSans.ttf") });

    b.installArtifact(exe);

    const run_step = b.step("run", "Run the app");
    const run_cmd = b.addRunArtifact(exe);
    run_step.dependOn(&run_cmd.step);

and then, here’s how I’m calling in my code to get the file:

const FONT = @embedFile("pixeloid_font");

Here’s the error I’m getting:

src/ui/core/ui.zig:12:25: error: unable to open 'pixeloid_font': FileNotFound
const FONT = @embedFile("pixeloid_font");

What did I do wrong here?

I’m assuming your @embedFile is inside the “ness” module but you have added the “pixeloid_font” import to the root module of the exe not the “ness” module.

4 Likes

That was it, thanks!