DL_LOAD_PATH cleared outside of module

I am writing a module Foo, which depends on ccalling some code in another part of my source tree. In my module, I push!(Libdl.DL_LOAD_PATH, "path/to/file"), and I’m able to find the .so according to some debugging.

However, outside of the module, after using Foo, Libdl.DL_LOAD_PATH is empty, and during compilation, the functions inside of Foo can’t find the requisite .so. Without the using/module barrier, everything seems okay (running lines from the REPL).

I can also successfully load the library if, after using Foo, I push! the right path again.

What’s going on? How can I fix this? An MWE seems hard to provide because it requires setting up a dev package and I’m not too familiar with the packaging system, but I can try.

Maybe you can define __init__ function for your module to push! the path and execute any other initialization steps at the runtime. Search __init__ on https://docs.julialang.org/en/v1/manual/modules/# to see explanations and examples.

1 Like

This solved my problem perfectly. Thanks so much for the pointer! I suppose I was confused about the difference between what happens at compile time and what happens at runtime, and this clarified the split perfectly.

Don’t fiddle with LD_LOAD_PATH. Use a full path in ccall instead.

const my_library = joinpath(@__DIR__, "relative/path/to/somewhere/else/in/my/source/tree/library.so")
function my_wrapper(...)
    ccall((:my_function, my_library), ...)
end