Search code examples
macosdylibdyldotoolinstall-name-tool

dyld: Library not loaded ... Reason: Image not found


When trying to run an executable I've been sent in Mac OS X, I get the following error

dyld: Library not loaded: libboost_atomic.dylib
  Referenced from: /Users/"Directory my executable is in"
  Reason: image not found
Trace/BPT trap:5

I have installed the boost libraries and they are located in /opt/local/lib. I think the problem has something to do with the executable only looking in the directory it is in as when I paste the 'libboost_atomic.dylib' in there, it doesn't mind about it anymore. Unfortunately then it complains it can't find the next boost library.

Is there an easy way to fix this?


Solution

  • Find all the boost libraries (where exefile is the name of your executable):

    $ otool -L exefile
    exefile:
            @executable_path/libboost_something.dylib (compatibility version 0.7.0, current version 0.7.0)
            /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 65.1.0)
            /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
    

    and for each libboost_xxx.dylib, do:

    $ install_name_tool -change @executable_path/libboost_something.dylib /opt/local/lib/libboost_something.dylib exefile
    

    and finally verify using otool again:

    $ otool -L exefile
    exefile:
            /opt/local/lib/libboost_something.dylib (compatibility version 0.7.0, current version 0.7.0)
            /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 65.1.0)
            /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
    

    Manpages: otool install_name_tool

    EDIT A while back I wrote a python script (copy_dylibs.py) to work out all this stuff automatically when building an app. It will package up all libraries from /usr/local or /opt/local into the app bundle and fix references to those libraries to use @rpath. This means you can easily install third-party library using Homebrew and package them just as easily.

    I have now made this script public on github.