Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is install_name_tool and otool necessary for Mach-O libraries in Mac Os X?

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

like image 499
Alex Avatar asked Feb 13 '12 15:02

Alex


People also ask

What is Otool Mac?

The otool command displays specified parts of object files or libraries. If the -m option is not used the file arguments may be of the form libx. a(foo.o), to request information about only that object file and not the entire library.

What is Rpath Mac?

In computing, rpath designates the run-time search path hard-coded in an executable file or library. Dynamic linking loaders use the rpath to find required libraries. Specifically, it encodes a path to shared libraries into the header of an executable (or another shared library).

What is install name?

The term install name refers to the exact path of the . dylib file in the end-user system so the runtime linker can find and load the dynamic library. The name can be either: Absolute, which is the case for system libraries. These are in the same place on both the end-user's and developer's system.


1 Answers

Apple has several ways of locating shared libraries:

  1. @executable_path : relative to the main executable
  2. @loader_path : relative to the referring binary
  3. @rpath : relative to any of a list of paths.

@rpath is the most recent addition, introduced in OS X 10.5.

If for instance you want to have your executable in Contents/MacOS and libraries in Contents/Libraries you could do the following:

install_name_tool -id @rpath/Libraries/lib_this.dylib   builddir/lib_this.dylib 

and in the top-level executable set rpath with:

install_name_tool -add_rpath @loader_path/..  myexecutable 

and:

install_name_tool -change builddir/lib_this.dylib @rpath/Libraries/lib_this.dylib myexecutable 

Note: that the first path after -change must match exactly what is currently in the binary.

If you get lost otool -l -v myexecutable will tell you what load commands exactly are currently in the executable.

See man dyld and man install_name_tool for more information.

like image 91
Willem Hengeveld Avatar answered Oct 07 '22 07:10

Willem Hengeveld