#Trouble linking raylib as a non system library

1 messages · Page 1 of 1 (latest)

deft igloo
#

I want to use raylib, which I have extracted to a folder thats part of my projects file system itself, but I don't know how.

Code:

const std = @import("std");
const ray = @cImport({
    @cInclude("/home/markus/Development/zig/bouncy_balls/lib/raylib-4.5.0_linux_amd64/include/raylib.h");
});

const SCREEN_WIDTH = 16 * 5;
const SCREEN_HEIGHT = 9 * 5;

pub fn main() !u8 {
    ray.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib test");
    defer ray.CloseWindow();

    std.time.sleep(std.time.ns_per_s);

    return 0;
}

build.zig:

const std = @import("std");

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

    const exe = b.addExecutable(.{
        .name = "bouncy_balls",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    exe.linkLibC();
    b.installArtifact(exe);
    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| run_cmd.addArgs(args);

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
    const unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const run_unit_tests = b.addRunArtifact(unit_tests);
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_unit_tests.step);
}

error:

error: ld.lld: undefined symbol: InitWindow
    note: referenced by main.zig:10
    note:               /home/markus/Development/zig/bouncy_balls/zig-cache/o/d10e83d63dbc8d4aeee25e25abdef346/bouncy_balls.o:(main.main)
error: ld.lld: undefined symbol: CloseWindow
    note: referenced by main.zig:11
    note:               /home/markus/Development/zig/bouncy_balls/zig-cache/o/d10e83d63dbc8d4aeee25e25abdef346/bouncy_balls.o:(main.main)
warped dust
#

You should link it as a system library

#

I.e with b.linkSystemLibrary

#

Or whatever it is

#

You just have to add run.addLibraryPath or something

#

linkSystemLibrary just checks all the folders that are included in it's library paths

deft igloo
#

oi oke

#

wait i just have the thing extracted as is, with the include folder in the same dir as the lib folder

#

so what do I add, the include, lib, or the folder where they both are in

warped dust
#

The lib has to be directly in the folder that you add to addLibraryPath

deft igloo
#

oke

deft igloo
#

oke i dont know what to do

rancid vessel
#

I'm not sure this is a good solution, but it works for me.

const std = @import("std");
const raylib_build = @import("raylib/src/build.zig");

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

    const raylib = raylib_build.addRaylib(b, target, optimize);

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

    exe.linkLibC();
    exe.linkLibrary(raylib);
    exe.addIncludePath("raylib/src");

    exe.install();

    const run_cmd = exe.run();

    run_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

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

    const exe_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&exe_tests.step);
}
#

With raylib inside the Zig project folder.

deft igloo
#

Im sorry I should have specified what im doing more precisely, I am using raylibs native c implementation

#

I am trying to just @cInclude the thing

warped dust
#

This is how I include some other c libs
c.zig:

pub usingnamespace @cImport({
    @cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "1");
    @cDefine("CIMGUI_USE_GLFW", "1");
    @cDefine("CIMGUI_USE_OPENGL3", "1");
    @cInclude("cimgui.h");
    @cInclude("generator/output/cimgui_impl.h");
    @cUndef("CIMGUI_USE_GLFW");
    @cUndef("CIMGUI_USE_OPENGL3");
    @cUndef("CIMGUI_DEFINE_ENUMS_AND_STRUCTS");

    @cInclude("GL/glew.h");
});
#

I have then added the include directory with exe.addIncludePath

#

Of course this isn't raylib, but it should be 1 to 1 mappable

deft igloo
#

Ive tried this and ive tried to import it but it still gives me the same ld.lld error thingy

#

should i send the program, build file, error and this time file structure again?

#

screw it ill do it, it cant hurt

#

Project structure

build.zig
lib
├── raylib-4.5.0_linux_amd64
│   ├── CHANGELOG
│   ├── include
│   │   ├── raylib.h
│   │   ├── raymath.h
│   │   └── rlgl.h
│   ├── lib
│   │   ├── libraylib.a
│   │   └── libraylib.so.4.5.0
│   ├── LICENSE
│   └── README.md
└── raylib-4.5.0_linux_amd64.tar.gz
src
└── main.zig

Build file

const std = @import("std");

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

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

    exe.linkLibC();
    exe.addIncludePath("/home/markus/Development/zig/bouncy_balls/lib/raylib-4.5.0_linux_amd64/include");

    b.installArtifact(exe);
    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| run_cmd.addArgs(args);

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
    const unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const run_unit_tests = b.addRunArtifact(unit_tests);
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_unit_tests.step);
}

main.zig

const std = @import("std");
const ray = @cImport({
    @cInclude("raylib.h");
});

const SCREEN_WIDTH = 16 * 5;
const SCREEN_HEIGHT = 9 * 5;

pub fn main() !u8 {
    ray.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib test");
    defer ray.CloseWindow();

    std.time.sleep(std.time.ns_per_s);

    return 0;
}
#

Error remains the same as in the original post itself

rancid vessel
azure spindle
#

your build.zig needs to have addLibraryPath and linkSystemLibrary in it if you want to dynamically link to raylib

#

i.e:

const std = @import("std");

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

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

    exe.addLibraryPath("raylib-4.5.0_linux_amd64/lib");
    exe.linkSystemLibrary("raylib");
    exe.addIncludePath("raylib-4.5.0_linux_amd64/include");
    exe.linkLibC();

    b.installArtifact(exe);
}
#

(i've removed the run step for terseness)

deft igloo
#

okay

#

So I think this is progress because I now get different errors lol

#

I get tons of these:

error: ld.lld: undefined symbol: __ppoll_chk
    note: referenced by rglfw.c
    note:               rglfw.o:(_glfwPollPOSIX) in archive /home/markus/Development/zig/bouncy_balls/lib/raylib-4.5.0_linux_amd64/lib/libraylib.a
error: ld.lld: undefined symbol: __poll_chk
    note: referenced by rglfw.c
    note:               rglfw.o:(_glfwPollPOSIX) in archive /home/markus/Development/zig/bouncy_balls/lib/raylib-4.5.0_linux_amd64/lib/libraylib.a
#

Looks to me like some dependencies of raylib itself are missing but idk what to do about it

#

fuck it i just saw that theres actually a build.zig in the source code so i just cloned the repo and now the question is how i link the entire thing

azure spindle
#

that looks like you're not linking libc to the exe

azure spindle
# deft igloo fuck it i just saw that theres actually a build.zig in the source code so i just...

that build.zig is just for building raylib standalone, if you want to statically link raylib you'll want the build.zig that's in raylib/src/build.zig and include it like so:

    const rl = @import("raylib/src/build.zig");
    const lib = rl.addRaylib(b, target, optimize, .{});
    exe.linkLibC();
    exe.addIncludePath("raylib/src/");
    exe.linkLibrary(lib);

edit: updated to include the options now add the end of the addRaylib function

deft igloo
#

yess thx

#

I now get errors about missing libraries which im downloading all the levels of

azure spindle
#

raylib should come with the dependencies it needs

deft igloo
#

it complains it cant find

error: ld.lld: unable to find library -lGL

deft igloo
#

i thought so too but it complains

azure spindle
#

opengl should be pre-installed on your system

#

are you on a bare arch or something?

deft igloo
#

voidlinux

#

idk i like the distrobution but so far i never did anything with libraries on it

azure spindle
#

yeah, void probably doesn't have opengl installed by default

#

just try adding libgl

deft igloo
#

okay

#

which one lol

#

all of em?

azure spindle
#

no idea, this is no longer a zig thing, you'll have to figure this out with someone else i'm afraid, not a void linux user

deft igloo
#

oki

warped dust
#

Seems like you need the mesa package

#

Am unsure of where you find your packages on void

#

But I believe I got the packages when I installed my graphics card drivers

#

Maybe this can help

┬─[co :~]─[T:1803ms]
╰─>$ sudo pacman -Qs libGL
local/glew 2.2.0-6
    The OpenGL Extension Wrangler Library
local/glib2 2.76.2-1
    Low level core library
local/glibmm 2.66.6-1
    C++ bindings for GLib
local/lib32-glib2 2.76.2-1
    Low level core library - 32-bit
local/lib32-libglvnd 1.6.0-1
    The GL Vendor-Neutral Dispatch library
local/lib32-mesa 23.0.3-1
    An open-source implementation of the OpenGL specification (32-bit)
local/lib32-nvidia-utils 530.41.03-1
    NVIDIA drivers utilities (32-bit)
local/libglvnd 1.6.0-1
    The GL Vendor-Neutral Dispatch library
local/mesa 23.0.3-1
    An open-source implementation of the OpenGL specification
local/nvidia-utils 530.41.03-1
    NVIDIA drivers utilities
#

These are what I get when I search for libGL, and it seems like the mesa and nvidia ones are the relevant ones

jagged edge
#

Hi, I have been trying to link raylib to my project from the submodule I have cloned and found this post. I followed the steps provided here but it seems like raylib updated the addRaylib function in their build.zig file to have a forth parameter of Options. Options seems to be a private struct in their build.zig. I am pretty new to zig so I am not quite sure how to pass in the forth parameter or link raylib now. thanks for any help. updated raylib build.zig: https://github.com/raysan5/raylib/blob/master/src/build.zig#L4 https://github.com/raysan5/raylib/blob/master/src/build.zig#L118

GitHub

A simple and easy-to-use library to enjoy videogames programming - raylib/src/build.zig at master · raysan5/raylib

azure spindle
#

Options is declared in that file, though it probably should be pub const

#

you can just call it with an empty tuple, .{}

#

as in, addRaylib(b, target, optimize, .{});