Not-Nik / raylib-zig

Manually tweaked, auto-generated raylib bindings for zig. https://github.com/raysan5/raylib
MIT License
682 stars 122 forks source link

Raygui can't be used when raylib is built as a shared library #147

Closed MassKlaus closed 1 month ago

MassKlaus commented 2 months ago

Error:

lld-link: undefined symbol: GuiPanel

Adding the .shared = true when using raygui breaks things

buid.zig:

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

main.zig

const rg = @import("raygui");
const rl = @import("raylib");

pub fn main() anyerror!void {
    // Initialization
    //--------------------------------------------------------------------------------------
    const screenWidth = 800;
    const screenHeight = 450;

    rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
    defer rl.closeWindow(); // Close window and OpenGL context

    rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!rl.windowShouldClose()) { // Detect window close button or ESC key
        // Update
        //----------------------------------------------------------------------------------
        // TODO: Update your variables here
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        rl.beginDrawing();
        defer rl.endDrawing();

        rl.clearBackground(rl.Color.white);

        const bounds = rl.Rectangle.init(50, 50, 100, 400);

        _ = rg.guiPanel(bounds, "Something");

        rl.drawText("Congrats! You created your first window!", 190, 200, 20, rl.Color.light_gray);
        //----------------------------------------------------------------------------------
    }
}

Main reason for trying to build raylib and raygui as a shared library is implementing hot reloading eventually

ZackeryRSmith commented 2 months ago

Hey @MassKlaus, have you made sure to add the raygui import in the build.zig? I copied your main file 1:1, but here is my build file (minus the web export code).

const std = @import("std");
const rlz = @import("raylib-zig");

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

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

    const raylib = raylib_dep.module("raylib");
    const raygui = raylib_dep.module("raygui");  // note this line
    const raylib_artifact = raylib_dep.artifact("raylib");

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

    exe.linkLibrary(raylib_artifact);
    exe.root_module.addImport("raylib", raylib);
    exe.root_module.addImport("raygui", raygui); // note this line

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

    b.installArtifact(exe);
}

Using Zig 0.13 and the latest version of these bindings, everything builds fine. Here is the result:

Screenshot 2024-09-09 at 6 17 12 PM
Not-Nik commented 2 months ago

raygui is added to the build regardless of configuration, just like any other raylib source file. As @ZackeryRSmith said, this is probably a build misconfig on your end.

MassKlaus commented 2 months ago

Sorry for the late reply but yeah, I forgot to include my full build script but it was the default zig-init build script with the raylib-zig bindings imports for both raylib and raygui. I copied your build script and still ran into the same issue. I am on windows, so maybe a compiler thing?

ZackeryRSmith commented 2 months ago

I am on windows, so maybe a compiler thing?

It is strange that this only occurs when building as a shared library. I don't think it's an issue with building on Windows. However if that is the case: the issue is likely with Raylib's build.zig and is a separate issue unrelated to this project.

I cannot reproduce this error on my machine sadly, so it's hard for me to debug. Although I'm a bit lost here... To be clear though: What version of Zig are you using and how are you installing the bindings.

Main reason for trying to build raylib and raygui as a shared library is implementing hot reloading eventually

I guess for the moment just ignore building as a shared library. Once I have the chance I will try debugging this on my Windows VM.

MassKlaus commented 2 months ago

I am using Zig 0.13.0 and the bindings are fetched as indicated in the ReadMe of the project. I've just reinstalled my system so I'll a chance later and test in wsl 2 when I can.

A lot of thanks!

Not-Nik commented 1 month ago

Any updates? Otherwise I'll close this, since it seems resolved and I can't repro it on my machine.

MassKlaus commented 1 month ago

I wasn't really able to figure out what was happening sadly, wsl doesn't have x11 and couldn't push for more so for now it's best to close till someone else has some similar issue