I am developing a Cocoa Application using the latest version of Xcode 4, I want to link dynamic libraries to my project (dylibs).
I read somewhere that adding the libraries in my project was not enough as I have to run install_name_tool and otool to make my project use the libraries that were bundled in my project.
I have read the manual pages for install_name_tool, but I do not understand WHY I have to do this.
How do libraries work? Especially interested in the part where the application and the libraries have paths that point to specific places in my machine, like /usr/local/lib/mylibrary.dylib when running otool -L
Apple has several ways of locating shared libraries:
@executable_path: relative to the main executable@loader_path: relative to the referring binary@rpath: relative to any of a list of paths.@rpathis the most recent addition, introduced in OS X 10.5.If for instance you want to have your executable in
Contents/MacOSand libraries inContents/Librariesyou could do the following:and in the top-level executable set
rpathwith:and:
Note: that the first path after
-changemust match exactly what is currently in the binary.If you get lost
otool -l -v myexecutablewill tell you what load commands exactly are currently in the executable.See
man dyldandman install_name_toolfor more information.