I have been struggling with this concept for a while and I cannot really understand what the difference is between -change
and -id
.The man page states
-id name
Changes the shared library identification name of a dynamic shared library to name. If the Mach-O binary is not a dynamic
shared library and the -id option is specified it is ignored.
-change old new
Changes the dependent shared library install name old to new in the specified Mach-O binary. More than one of these options
can be specified. If the Mach-O binary does not contain the old install name in a specified -change option the option is
ignored.
So far I have experimented with -change
. Suppose I have the following structure
Test.App
|_Contents
|_MacOS
| |_test -----> item A
|_Library
|_test_library.dylib --->item B
|_another_library.dylib --->item C
Now suppose I ran the following on itemB
$ otool -L test_library.dylib
test_library.dylib
/some/path/another_library.dylib -->item D
The above result indicates that test_library.dylib
depends on another_library.dylib
now if I needed to change the location of another_library.dylib
I would do this
install_name_tool -change /some/path/another_library.dylib some/new/path/another_library.dylib test_library.dylib
this would change the location of item D. My question is what does install-name_tool -id
do and when do I use that ?
id
is used at link time andinstall name
is used at runtime. They are all information provided for the linker to locate the dylib. I followed this tutorial.Let me show an example,
As you can see, the first line is the
id
. Let's link with libb.dylib,Just notice the second line, the
id
for liba.dylib is used here. Let's change the id tofoo/liba.dylib
and link again,So you see the
-D
and-L
all outputs the currentid
asfoo/liba.dylib
.Let's link again with liba.dylib again,
See the difference? The run time location to find liba.dylib is changed to
foo/liba.dylib
at the second line.Basically, it tells libb.dylib to find liba.dylib from
current_dir/foo
install-name_tool -id
is used for change theinstall name
ofdylib
, you can use theotool -D
see a dylibinstall name
in the terminal, it will show the default value for you, the/some/path/another_library.dylib
is the defaultinstall name
of another_library.dylib
, of course, you can change it useinstall-name_tool -id
in the terminal, just use like this in terminalnow,you use the
otool -D /some/path/another_library.dylib
, you will find theinstall name
is/some/path/another_library_newname.dylib
here is my example in picture
My understanding is:
-id
: This sets the "install name" that will be used when linking against the dynamic library. It would be run on the target dynamic library file.-change
: This changes the "install name" after linking and would be run on the executable or dynamic library that links against the target dynamic library.