# Libdl.dlopen a dylib that depends on another dylib

**URL:** <https://discourse.julialang.org/t/libdl-dlopen-a-dylib-that-depends-on-another-dylib/13673>\
**Category:** General Usage\
**Created:** [August 18, 2018, 6:58pm UTC](https://discourse.julialang.org/t/libdl-dlopen-a-dylib-that-depends-on-another-dylib/13673 "2018-08-18T18:58:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![felipenoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipenoris/32/553_2.png) [@felipenoris](https://discourse.julialang.org/u/felipenoris)\
**Post date:** [August 18, 2018, 6:58pm UTC](https://discourse.julialang.org/t/libdl-dlopen-a-dylib-that-depends-on-another-dylib/13673/1 "2018-08-18T18:58:02Z")

</div>

I’m working on a fork of `Mongo.jl` and I set up a [repo](https://github.com/felipenoris/mongo-c-driver-builder) 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
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
Stacktrace:
 [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](https://github.com/felipenoris/mongo-c-driver-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.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [August 18, 2018, 7:34pm UTC](https://discourse.julialang.org/t/libdl-dlopen-a-dylib-that-depends-on-another-dylib/13673/2 "2018-08-18T19:34:27Z")

</div>

[![](https://global.discourse-cdn.com/julialang/original/3X/a/0/a0a2da7cd4e08758770dffd9d60fefe591f8c9f0.jpeg "JuliaCon 2018 | 10 tips on how to build better binaries | Elliot Saba") ](https://www.youtube.com/watch?v=2e0PBGSaQaI)

---

<div class="post-metadata">

**Author:** ![felipenoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipenoris/32/553_2.png) [@felipenoris](https://discourse.julialang.org/u/felipenoris)\
**Post date:** [August 18, 2018, 9:06pm UTC](https://discourse.julialang.org/t/libdl-dlopen-a-dylib-that-depends-on-another-dylib/13673/3 "2018-08-18T21:06:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![felipenoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipenoris/32/553_2.png) [@felipenoris](https://discourse.julialang.org/u/felipenoris)\
**Post date:** [August 18, 2018, 11:54pm UTC](https://discourse.julialang.org/t/libdl-dlopen-a-dylib-that-depends-on-another-dylib/13673/4 "2018-08-18T23:54:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![felipenoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipenoris/32/553_2.png) [@felipenoris](https://discourse.julialang.org/u/felipenoris)\
**Post date:** [August 20, 2018, 2:06am UTC](https://discourse.julialang.org/t/libdl-dlopen-a-dylib-that-depends-on-another-dylib/13673/5 "2018-08-20T02:06:22Z")

</div>

Solved. It turns out it was a bug in BinaryBuilder.jl . Ref [audit should check all dynamic links by felipenoris · Pull Request #330 · JuliaPackaging/BinaryBuilder.jl · GitHub](https://github.com/JuliaPackaging/BinaryBuilder.jl/pull/330).
