No module named 'X available within module Y

I’m trying to import this sfml wrapper library into my project.I’ve added it to my build.zig.zon through zig fetch and then into the build.zig and was able to build my project, but when I try to run it I get the following error:

run command

zig run src/main.zig -lc -freference-trace=7

error:

src\main.zig:8:21: error: no module named 'sfml' available within module main                                                                                                                             
const sfml = @import("sfml");                                                                                                                                                                              
                    ^~~~~~                                                                                                                                                                                
referenced by:
    sfml: src\main.zig:11:18
    usingnamespace_4: src\main.zig:16:20
    sf: src\main.zig:10:12
    main: src\main.zig:31:22
    main: zig-path\lib\std\start.zig:656:37
    comptime: zig-path\lib\std\start.zig:58:30
    start: zig-path\lib\std\std.zig:97:27
    1 reference(s) hidden; use '-freference-trace=8' to see all references

build.zig

const std = @import("std");
const sfml = @import("sfml");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

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

    const dep = b.dependency("sfml", .{}).module("sfml");
    exe.root_module.addImport("sfml", dep);
    dep.addIncludePath(b.path("../CSFML/include/"));
    exe.addLibraryPath(b.path("../CSFML/lib/msvc/"));
    exe.linkLibC();
    exe.linkSystemLibrary("csfml-graphics");
    exe.linkSystemLibrary("csfml-system");
    exe.linkSystemLibrary("csfml-window");
    exe.linkSystemLibrary("csfml-audio");

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

main.zig

const sf = struct {
    const sfml =  @import("sfml");
    usingnamespace sfml;
    usingnamespace sfml.audio;
    usingnamespace sfml.graphics;
    usingnamespace sfml.window;
    usingnamespace sfml.system;
};

pub fn main() !void {
    var window = try sf.RenderWindow.create(.{ .x = 800, .y = 600 }, 32, "SFML Window", sf.Style.defaultStyle, null);
    defer window.destroy();

   
    while (window.isOpen()) {
        while (window.pollEvent()) |event| {
            if (event == .closed)
                window.close();
        }

        window.clear(sf.Color.Black);
        window.display();
    }
}

Can someone explain how to solve this issue? I have the SFML and CSFML libraries in my project folder and the DLL files in my zig-out/bin folder.

Post the whole error.

Are you sure it’s not coming from the const sfml = @import("sfml"); in the build.zig?

That is the whole error. I also posted the command I used to run it and the call stack.

This might be silly on my part, but does the following work?

const sfml =  @import("sfml");

const sf = struct {
    usingnamespace sfml;
    usingnamespace sfml.audio;
    usingnamespace sfml.graphics;
    usingnamespace sfml.window;
    usingnamespace sfml.system;
};

I also tried this before posting and it did not work.

I’m confused because this line from the error doesn’t appear in the main.zig you posted:

const sfl = @import("sfml"); 

(note sfl instead of sfml)

Sorry that was a copy and paste error on my part. I fixed it but I’ll past it here as well.

src\main.zig:10:26: error: no module named 'sfml' available within module main
    const sfml = @import("sfml");

Oh, I just noticed:

That does not go through the build system, so the import does not get added.

Instead, run:

zig build run

(you can pass --verbose if you want to see the underlying commands that get run)

2 Likes