Skip to content
/ airelien.dev
Go back
Aurélien AMSELLEM

Lucebox on Olares One — Episode 3: LIBRARY_PATH isn't what you think it is

We added LIBRARY_PATH and a libcuda.so.1 symlink, fired off another 2h compile, and ld dropped the same error. Why? Because LIBRARY_PATH doesn't resolve indirect dependencies. You need -Wl,-rpath-link.

Episode 1: we discover Lucebox and I decide to package it for Olares. Episode 2: first build, 2h of compile for 11 undefined references to cuMemCreate, cuMemMap, etc.

The fix I applied: LIBRARY_PATH pointing at /usr/local/cuda/lib64/stubs, plus a libcuda.so → libcuda.so.1 symlink to the stub (the fake libcuda NVIDIA ships, which is only there for linking). Seems logical. I rerun.

2h later

New compile, new link, and then:

/usr/bin/ld: warning: libcuda.so.1, needed by libggml-cuda.so.0.9.11, not found
/usr/bin/ld: libggml-cuda.so.0.9.11: undefined reference to `cuMemCreate'
... 11 identical undefined references

The exact same error, letter for letter, as if I had done nothing.

Debugging rule #1: if you think you fixed a problem and it comes back identical, you didn’t fix the actual problem. Time to read the manual.

What LIBRARY_PATH actually does

First, a word on dynamic linking. An executable doesn’t carry all its code: it keeps a list of the shared libraries (the .so files) it needs, and the system loader finds and plugs them in when the program starts. Those libraries can depend on other libraries in turn. At build time, the linker already checks that whole chain, to make sure every called function exists somewhere.

LIBRARY_PATH is an environment variable that gcc and clang use to find the libraries named directly on the link command. If you run gcc main.c -lfoo and libfoo.so sits in a directory listed in LIBRARY_PATH, ld will find it.

But if you run gcc main.c -lbar, and libbar.so itself depends on another library, libfoo.so, then LIBRARY_PATH doesn’t help. ld will look for libfoo.so in its standard system search path (/lib, /usr/lib, /usr/lib/x86_64-linux-gnu, etc.) and nowhere else.

That’s called an indirect dependency, and it’s exactly our case: we link test_dflash, which depends on libggml-cuda.so, which depends on libcuda.so.1. ld finds libggml-cuda.so (direct) but not libcuda.so.1 (indirect), because it doesn’t look in LIBRARY_PATH for indirect dependencies.

The ld warning said so in plain words:

not found (try using -rpath or -rpath-link)

I had read it without really understanding it. The ld docs describe our situation exactly:

-rpath-link=DIR: When using ELF or SunOS, one shared library may require another. […] If -rpath-link is specified, the linker will use that for indirect resolution.

The two options the warning suggests don’t do the same thing. -rpath writes a directory into the binary, so the loader searches it for libraries every time the program starts. -rpath-link is only used during linking, to find indirect dependencies, and leaves no trace in the binary. The second one is what we need here: ld has to find the stub during the build, but at runtime it’s the host’s real libcuda.so.1 that must be loaded, not the stub.

The real fix

Pass the stubs path to the linker through CMAKE_EXE_LINKER_FLAGS (for executables) and CMAKE_SHARED_LINKER_FLAGS (for shared libraries), not through LIBRARY_PATH. The -Wl, prefix tells the compiler to hand the option over to the linker as is:

RUN cmake -B build -S . \
    -DCMAKE_BUILD_TYPE=Release \
    -DGGML_CUDA=ON \
    -DCMAKE_CUDA_ARCHITECTURES="120" \
    -DCMAKE_EXE_LINKER_FLAGS="-Wl,-rpath-link,/usr/local/cuda/lib64/stubs" \
    -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-rpath-link,/usr/local/cuda/lib64/stubs" \
    && cmake --build build --target test_dflash -j $(nproc)

Note that I also went from "86;89;120" to just "120". I’m not distributing this image to the world: it will only run on my Olares One, on sm_120. That cuts compile time by ~3× without losing anything. If I ever need a wider target, I’ll add the other architectures back.

56 minutes later

[ 98%] Built target dflash27b
[100%] Building CXX object CMakeFiles/test_dflash.dir/test/test_dflash.cpp.o
[100%] Linking CXX executable test_dflash
[100%] Built target test_dflash
#13 DONE 3337.7s

Yes! Built target test_dflash: the DFlash CLI binary is compiled. 56 min this time (the CUDA cache from the previous build is partly reused, and there’s only one architecture left). Not bad at all.

Except test_dflash is just the Lucebox bench CLI. To serve the model over HTTP with an OpenAI-compatible API, I need llama-server, which compiles from the deps/llama.cpp submodule of the Lucebox fork (a git submodule is another repository embedded inside the first one). Build #2.

And surprise: it’s not the same cmake invocation, it’s a sub-project, and I haven’t carried the -rpath-link over. So when I kick off the llama-server build…

(You see where this is going.)

Episode 4: 1h later, ld dumps the exact same 11 undefined references. See you next time!


Disclosure — All the benchmarks in this post run on my own Olares One. If the content was useful and you’re considering one, ordering through this referral link gets you $300 off ($4,699 instead of $4,999), stackable with the $300 Black Friday discount starting November 17: $600 off in total ($4,399). I’m mentioning this out of transparency — and yes, incidentally, it helps keep the blog alive (hosting, domain, and the time I spend writing here).

Share this post on:

Comments