Archived Forum Post

Index of archived forum posts

Question:

How to Distribute a dylib with a Mac OS X Application?

Oct 04 '13 at 11:28

We are implementing Chilkat cocoa library for ftp. All works well if we put the library in "/usr/local/lib/". But when we try to run without it it crashes with error

dyld: Library not loaded: /usr/local/lib/libchilkatCocoaDyn.dylib

Referenced from: /Users/abc/Library/Developer/Xcode/DerivedData/xyz/Build/Products/Debug/something.app/Contents/MacOS/

Reason: image not found

We know its common issue for dynamic libraries. Can you please help us with this? How should i bundle libchilkatCocoaDyn.dylib within the app?


Answer

On the Mac a dynamic library (dylib) has an "install name". The install name is a path baked into the dynamic library that says where to find the library at runtime. When you link against the dylib this path is saved in your binary so that your binary can find the dylib at runtime.

You can see the install name of a dylib by using otool. For example:

$ otool -D libchilkatCocoaDyn.dylib
libchilkatCocoaDyn.dylib:
/usr/local/lib/libchilkatCocoaDyn.dylib

This means that unless you set the DYLD_LIBRARY_PATH environment variable to allow the runtime linker find the dylib, the dylib must be placed in the exact location as specified by the install name.

However, you can change the install name of a dylib by using the install_name_tool utility. You can also use the @loader_path keyword to make it relative to an install directory.

I would recommend becoming familiar with the install_name_tool command and it's various options. For example, this command changes the install name of libchilkatCocoaDyn.dylib to be relative to the location of the binary using it:

$ install_name_tool -id "@loader_path/dylibs/libchilkatCocoaDyn.dylib" libchilkatCocoaDyn.dylib