Search code examples
objective-ccocoaxcode5dylib

Xcode 5 how to Linking Dynamic Libraries from App Bundle


I want to distribute some libraries in to my OS X application bundle, last two days i am working on this however couldn't make it. until now what i did.

with using install name tool i have fixed library paths. additionally in time i have tried @loader_path/../Libraries and @executable_path/../Libraries as well.

otool -L libMagickWand-6.Q16.2.dylib 
@rpath/../Libraries/libMagickWand-6.Q16.2.dylib (compatibility version 3.0.0, current version 3.0.0)
    @rpath/../Libraries/libMagickCore-6.Q16.2.dylib (compatibility version 3.0.0, current version 3.0.0)
    @rpath/../Libraries/libfreetype.6.dylib (compatibility version 18.0.0, current version 18.2.0)
    @rpath/../Libraries/libbz2.1.0.dylib (compatibility version 1.0.0, current version 1.0.5)
    @rpath/../Libraries/libz.1.2.5.dylib (compatibility version 1.0.0, current version 1.2.5)
    @rpath/../Libraries/libltdl.7.dylib (compatibility version 11.0.0, current version 11.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)

in project targets / Build Phases / Add New Build Phase / Add Copy Files build Phase and copied all dynamic libraries to my app bundle.

enter image description here

that worked well, I can see the libraries are in the app bundle.

enter image description here

then adding @rpath/../Libraries/ to Build Settings / Runpath Search Paths

enter image description here

but still getting error message..

ld: library not found for -lMagickWand-6.Q16.2 clang: error: linker command failed with exit code 1 (use -v to see invocation)

if i add direct path lets say libraries are located in /User/username/libs/ to Library Search Paths in build settings it works.

enter image description here

am i missing something?


Solution

  • Content/Libraries is not a standard directory within an app bundle; use Contents/Frameworks instead (.dylibs are allowed in that directory just the same as .frameworks).

    Set the Install Name of each library to @rpath/libWhatever.dylib and set the Runpath Search Path of the executable (in Contents/MacOS) to @loader_path/../Frameworks.

    For library interdependencies then Runpath Search Path will need to be simply @loader_path so dependent libraries can be loaded from the same directory.

    EDIT: People might find the copy_dylibs.py script in this repo useful for copying third-party .dylibs into the App Bundle. It recursively hunts for libraries that need copying and corrects the Install Name of the libraries as well as code-signing them.