Libdl.dlopen a dylib that depends on another dylib

I’m working on a fork of Mongo.jl and I set up a repo just to build the MongoDB C library, which provides the C libs libbson and libmongoc.

I’m having trouble loading libmongoc dynamic library on OSX because of path issues, since it looks like it depends on libbson and the libraries are inside the deps folder of Mongo package, and BinaryProvider uses the fullpath of the dynamic lib to dload it.

So, just to make this clear, there is a path “~/.julia/v0.6/Mongo/deps/usr/lib” that contains the files I’m interested in: “libbson-1.0.0.dylib” and “libmongoc-1.0.0.dylib”.

Trying to load libmongoc fails because it can’t find libbson:

julia> Libdl.dlopen(joinpath(homedir(), ".julia/v0.6/Mongo/deps/usr/lib/libmongoc-1.0.0.dylib"))
ERROR: could not load library "/Users/noronha/.julia/v0.6/Mongo/deps/usr/lib/libmongoc-1.0.0.dylib"
dlopen(/Users/noronha/.julia/v0.6/Mongo/deps/usr/lib/libmongoc-1.0.0.dylib, 1): Library not loaded: /workspace/destdir/lib/libbson-1.0.0.dylib
  Referenced from: /Users/noronha/.julia/v0.6/Mongo/deps/usr/lib/libmongoc-1.0.0.dylib
  Reason: image not found
 [1] dlopen(::String, ::UInt32) at ./libdl.jl:97 (repeats 2 times)

One interesting thing about this is that the error message talks about “/workspace/destdir/lib” folder, which I guess is the folder used by Travis builder.

One way I get this working is by putting “~/.julia/v0.6/Mongo/deps/usr/lib” on env variable DYLD_LIBRARY_PATH, but I find this somewhat intrusive code to put in a julia library.

Another way is to execute Julia inside the dynamic library directory.

Is there some way around this? I would like to dlopen libmongoc without messing with system environment variables.

I should also point out that this problem doesn’t happen on Linux.

I guess I’m following all those steps. I’m using BinaryBuilder.jl and Travis to build the library.

Does BinaryBuilder’s audit module try to fix this during check_dynamic_linkage ?

Solved. It turns out it was a bug in BinaryBuilder.jl . Ref