#build/linking issues with raylib

1 messages · Page 1 of 1 (latest)

pale canopy
#

zig version: 0.12.0-dev.2075+f5978181e

I took the following code from https://ziglang.org/learn/samples/:

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

pub fn main() void {
    const screenWidth = 800;
    const screenHeight = 450;

    ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
    defer ray.CloseWindow();

    ray.SetTargetFPS(60);

    while (!ray.WindowShouldClose()) {
        ray.BeginDrawing();
        defer ray.EndDrawing();

        ray.ClearBackground(ray.RAYWHITE);
        ray.DrawText("Hello, World!", 190, 200, 20, ray.LIGHTGRAY);
    }
}```

and tried building with `zig build-exe cimport.zig -lc -lraylib` as suggested. I got the following error:
```error: unable to find Dynamic system library 'raylib' using strategy 'paths_first'. searched paths: none```

after some trial and error, I came up with this: `zig build-exe main.zig -I./ -L./ -lc -lraylib`. I think I'm closer but it gives a bunch of `lld-link: undefined symbol` errors. I've suffered in the past with raylib and linking in c; if I remember correctly I may need more files to link, but I really don't know. Could anyone help?
patent notch
pale canopy
#

I've actually stumbled upon an old project of mine and found that I linked opengl32, gdi32, and winmm which that binding also seems to do, yet when I do the command zig build-exe main.zig -I./ -L./ -lc -lraylib -lopengl32 -lgdi32 -lwinmm it still gives the same errors. I think there might be an issue with the paths I'm using..?

pale canopy
patent notch
#

probably need -L/path/to/those/libs, that's why it's better to use a build.zig

pale canopy
patent notch
#

What are the names of the undefined symbols?

pale canopy
#

InitWindow, SetTargetFPS, WindowShouldClose, BeginDrawing, ClearBackground, DrawText, and EndDrawing

#

all the functions that would be in ray

patent notch
#

so you need to know where raylib is and -L/path/to/raylib_dir

pale canopy
#

raylib_dir as in the git repo/release or the lib file?

sharp root
#

I would also recommend the build system


pub fn build(b: *std.Build) void {
   const exe = b.addExecutable(.{
       .name = "main",
       .root_source_file = .{ .path = "main.zig" },
       .target = b.host,
   });

   exe.linkSystemLibrary("raylib");
   exe.linkLibC();

   b.installArtifact(exe);
}```
patent notch
#

raylib.dll

pale canopy
#

oh.. .dll? I don't think I have that

pale canopy
pale canopy
#

ah yes I see, so the source code version doesn't contain a .dll

#

is it possible to make that myself or should I just go and get a precompiled version?

sharp root
#

the .a file that is

#

on unix likes they generally have either a .a or .so file extension

#

or rather I should say a .so file is equavalent to a .dll file

#

.a are static libraries

pale canopy
#

can I not make it work with one though?

#

I'm pretty sure I have used libraylib.a in previous c projects and it worked fine.

sharp root
#

Raylib allows static linking so you can link to that and it should work just fine

pale canopy
#

that's the thing though, it doesn't 🥲

#

gcc main.c -o main.exe -Wall -std=c99 -I include/ -L lib/ -lraylib -lopengl32 -lgdi32 -lwinmm; ./main.exe here is a command I used in a previous project in c, and this worked with just a raylib.h and libraylib.a file (and the .c file of course)

#

the include/ and lib/ were there to make the file structure a bit clearer, but I'm basically doing the same thing in zig with this command: zig build-exe main.zig -I./raylib-5.0.0/src/ -L./raylib-5.0.0/src/ -lc -lraylib -lopengl32 -lgdi32 -lwinmm, yet here it doesn't work

patent notch
#

you need the -L/path/to libraylib.a

pale canopy
#

and -I/path/to/ raylib.h, correct?

patent notch
#

yes, that'd be -I./raylib-5.0.0/src/

pale canopy
#

so my (updated, simpler) file structure looks like this:

  main.zig
  libraylib.a
  raylib.h```
I run the following command: `zig build-exe main.zig -I./ -L./ -lc -lraylib -lopengl32 -lgdi32 -lwinmm`
and I still get `lld-link: undefined symbol` errors
#

thank you for your help, I'll go to sleep for now. let me know if you have found any solutions ;)

sharp root
#

if you download the release build from the raylib site and link to the raylib.so file in /usr/local/lib the command zig build-exe main.zig -lc -lraylib which is what the zig site gives will work.

patent notch
#

keep in mind they're using Windows

sharp root
#

Sorry missed that

pale canopy
#

yes, sorry if I'm not being clear. here is everything summed up so far:

os: windows (11)
zig version: 0.12.0-dev.2075+f5978181e
raylib version: 5.0, I've downloaded the source version from github and self compiled it to make a libraylib.a file

here is my file structure:

  main.zig
  libraylib.a
  raylib.h```

and this is the command I'm using: `zig build-exe main.zig -I./ -L./ -lc -lraylib -lopengl32 -lgdi32 -lwinmm`
I'm getting `lld-link: undefined symbol` errors
sharp root
#

Sorry on windows I'm not much help

pale canopy
# pale canopy yes, sorry if I'm not being clear. here is everything summed up so far: os: win...

I created a main.c file in the root folder, converted the zig code to c:

#include "raylib.h"

int main() {
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");

    SetTargetFPS(60);

    while (!WindowShouldClose()) {
        BeginDrawing();

        ClearBackground(RAYWHITE);
        DrawText("Hello, World!", 190, 200, 20, LIGHTGRAY);

        EndDrawing();
    }

    CloseWindow();
}```

and compiled it with `gcc main.c -o main.exe -I./ -L./ -lraylib -lopengl32 -lgdi32 -lwinmm`. The `.exe` runs as expected.
#

is this an issue with the zig version I'm using?

#

since I'm not sure what else could be the problem

pale canopy
#

oh interesting, using my own installation of gcc works, but using zig cc does not.

#

it's throwing the same kind of error, lld-link: error: undefined symbol

pale canopy
#

anyone know why this is? I've even tried zig version 0.11.0 but zig cc is not working...

patent notch
#

smells of ABI; try compiling raylib with zig cc see if it links

pale canopy
#

hm, what command do I use to do this? I only know how to do this with c files..

pale canopy
#

ah okay, I think I figured it out. I'm assuming the raylib.lib is going to work the same way as libraylib.a

#

yes, great! it totally worked