ziglibs / zgl

Zig OpenGL Wrapper
MIT License
425 stars 62 forks source link

Linking fails with 'undefined reference to' libdl functions. #61

Closed Linouth closed 2 years ago

Linouth commented 2 years ago

I had mach-glfw and zgl working together nicely, but at some point linking started to fail with the following errors;

LLD Link... ld.lld: error: /lib64/libepoxy.so: undefined reference to dlerror@GLIBC_2.34 [--no-allow-shlib-undefined]
ld.lld: error: /lib64/libepoxy.so: undefined reference to dlopen@GLIBC_2.34 [--no-allow-shlib-undefined]
ld.lld: error: /lib64/libepoxy.so: undefined reference to dlsym@GLIBC_2.34 [--no-allow-shlib-undefined]
error: FileNotFound

zig build --verbose-link gives

LLD Link... ld.lld -error-limit=0 -O0 -z stack-size=16777216 --gc-sections -znow -m elf_x86_64 -o /home/marten/src/motorsim/zig-cache/o/c9c8140870e3b12f9dc1b339e9fa06f3/motorsim /home/marten/.cache/zig/o/23f7278b09980272ad5975c0fb2e4096/Scrt1.o /home/marten/.cache/zig/o/b32b64a8fb73995fc5135714d247ac2e/crti.o -rpath /home/marten/.local/share/hexops/sdk-linux-x86_64/root/usr/lib/x86_64-linux-gnu -rpath /lib64 -rpath /lib -rpath /usr/lib64 -rpath /usr/lib -L /home/marten/.local/share/hexops/sdk-linux-x86_64/root/usr/lib/x86_64-linux-gnu -L /usr/local/lib64 -L /usr/local/lib -L /usr/lib/x86_64-linux-gnu -L /lib64 -L /lib -L /usr/lib64 -L /usr/lib -L /lib/x86_64-linux-gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 /home/marten/src/motorsim/zig-cache/o/7046d94a8a3c2cbb537d667ce7685374/libglfw.a /home/marten/.local/share/hexops/sdk-linux-x86_64/root/usr/lib/x86_64-linux-gnu/libXau.a /home/marten/.local/share/hexops/sdk-linux-x86_64/root/usr/lib/x86_64-linux-gnu/libXdmcp.a /home/marten/src/motorsim/zig-cache/o/c9c8140870e3b12f9dc1b339e9fa06f3/motorsim.o /home/marten/.cache/zig/o/027e12d25ebee0ab6cb34760a80829ce/libcompiler_rt.a --as-needed -lX11 -lxcb -lepoxy /home/marten/.cache/zig/o/c224969826294d4bdc6cb26db6cc24a8/libm.so.6 /home/marten/.cache/zig/o/c224969826294d4bdc6cb26db6cc24a8/libpthread.so.0 /home/marten/.cache/zig/o/c224969826294d4bdc6cb26db6cc24a8/libc.so.6 /home/marten/.cache/zig/o/c224969826294d4bdc6cb26db6cc24a8/libdl.so.2 /home/marten/.cache/zig/o/c224969826294d4bdc6cb26db6cc24a8/librt.so.1 /home/marten/.cache/zig/o/c224969826294d4bdc6cb26db6cc24a8/libld.so.2 /home/marten/.cache/zig/o/c224969826294d4bdc6cb26db6cc24a8/libutil.so.1 /home/marten/.cache/zig/o/2af95d2dee926e689433b2817020554f/libc_nonshared.a /home/marten/.cache/zig/o/de4ee0f5effa10017ceca863311bedfd/crtn.o

I am using Zig version 0.10.0-dev.3475+b3d463c9e with the following in my build script

    const exe = b.addExecutable("motorsim", "src/main.zig");
    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.install();

    exe.addPackagePath("glfw", "libs/mach-glfw/src/main.zig");
    glfw.link(b, exe, .{});

    exe.addPackagePath("zgl", "libs/zgl/zgl.zig");
    exe.linkSystemLibrary("dl");
    exe.linkSystemLibrary("epoxy");

If I understand correctly, my libepoxy version depends on the GLIBC_2.34 symbols, while the included libdl.so.2 file being linked has GLIBC_2.2.5. I do not understand why the compiler does not grab the available system libraries, which do seem to have to correct symbols in them.

Setting the target with -Dtarget=x86_64-linux-gnu.2.34, as suggested to someone with the same problem, results in

./libs/zgl/c.zig:6:5: error: C import failed
    @cImport({
    ^
./zig-cache/o/fca946021a1176b5ad82bf37c8570071/cimport.h:1:10: note: 'epoxy/gl.h' file not found
#include <epoxy/gl.h>
         ^
./libs/zgl/zgl.zig:228:6: error: container '.zgl.c' has no member called 'glClear'
    c.glClear(@as(types.BitField, if (mask.color) c.GL_COLOR_BUFFER_BIT else 0) |

I do not know how to proceed right now. Someone in the Zig discord server had the same issue, and for him bulding libepoxy himself seemed to work. However, it should be able to use the installed system libraries.

BratishkaErik commented 2 years ago

I do not understand why the compiler does not grab the available system libraries, which do seem to have to correct symbols in them.

See https://github.com/ziglang/zig/issues/6469

./libs/zgl/c.zig:6:5: error: C import failed
    @cImport({
    ^
./zig-cache/o/fca946021a1176b5ad82bf37c8570071/cimport.h:1:10: note: 'epoxy/gl.h' file not found
#include <epoxy/gl.h>
         ^
./libs/zgl/zgl.zig:228:6: error: container '.zgl.c' has no member called 'glClear'
    c.glClear(@as(types.BitField, if (mask.color) c.GL_COLOR_BUFFER_BIT else 0) |

Do you have libepoxy headers (usually package libepoxy-dev or smth.) installed on your system?

Linouth commented 2 years ago

Yeah. I'm using Arch which has the headers included in the libepoxy package.

BratishkaErik commented 2 years ago

Yeah. I'm using Arch which has the headers included in the libepoxy package.

Ok, maybe clearing cache will help you

$ rm -r ~/.cache/zig/ where-project/zig-cache/
Linouth commented 2 years ago

Same issue. Though, explicitly adding exe.addSystemIncludePath("/usr/include/"); gives the error ld.lld: error: unable to find library -lepoxy. So it does seem that the compiler has trouble finding the lib and header files..?

BratishkaErik commented 2 years ago

Try this (and clear cache):

    const exe = b.addExecutable("motorsim", "src/main.zig");
    exe.setTarget(target);
    exe.setBuildMode(mode);

    exe.addPackagePath("glfw", "libs/mach-glfw/src/main.zig");
    glfw.link(b, exe, .{});

    exe.addPackagePath("zgl", "libs/zgl/zgl.zig");
    exe.linkSystemLibrary("dl");
    exe.linkSystemLibrary("epoxy");

    exe.install();
Linouth commented 2 years ago

Same include error.

BratishkaErik commented 2 years ago

Same include error.

Try this (and clear cache):

    const exe = b.addExecutable("motorsim", "src/main.zig");

    exe.addPackagePath("glfw", "libs/mach-glfw/src/main.zig");
    glfw.link(b, exe, .{});

    exe.addPackagePath("zgl", "libs/zgl/zgl.zig");
    exe.linkSystemLibrary("dl");
    exe.linkSystemLibrary("epoxy");

    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.install();

It works for me without -Dtarget=blabla etc.

Linouth commented 2 years ago

That did it! I had no idea that setTarget and setBuildMode are place dependent in the build file?? Thanks!