I am making a package manager, and would like to ensure the packages use the correct dependent library by setting `rpath` option at compile time. But it surprised me in OS X (10.11), here is the problem:
$ otool -L /opt/starman/software/gcc/6.1.0/83894f21d07366be296600ec031ae4f6241381d9/libexec/gcc/x86_64-apple-darwin15.5.0/6.1.0/f951
/opt/starman/software/gcc/6.1.0/83894f21d07366be296600ec031ae4f6241381d9/libexec/gcc/x86_64-apple-darwin15.5.0/6.1.0/f951:
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/opt/starman/software/isl/0.17.1/104994def2b7fb2dae7950b42205eb718a46ee0c/lib/libisl.15.dylib (compatibility version 18.0.0, current version 18.1.0)
/opt/starman/software/mpc/1.0.3/6058925218009b8ab17e07333dc54de334134f6e/lib/libmpc.3.dylib (compatibility version 4.0.0, current version 4.0.0)
/opt/starman/software/mpfr/3.1.4/f142dfcda3b56650a8c9cfe2fdd09ffdf7283a00/lib/libmpfr.4.dylib (compatibility version 6.0.0, current version 6.4.0)
/opt/starman/software/gmp/6.1.0/0ec8ef118d09cb33f83559685d006f56a74f865c/lib/libgmp.10.dylib (compatibility version 14.0.0, current version 14.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
see `gmp` version is `14.0.0`, but I couldn't run a command `f951`
$ /opt/starman/software/gcc/6.1.0/83894f21d07366be296600ec031ae4f6241381d9/libexec/gcc/x86_64-apple-darwin15.5.0/6.1.0/f951
dyld: Library not loaded: /opt/starman/software/gmp/6.1.0/0ec8ef118d09cb33f83559685d006f56a74f865c/lib/libgmp.10.dylib
Referenced from: /opt/starman/software/gcc/6.1.0/83894f21d07366be296600ec031ae4f6241381d9/libexec/gcc/x86_64-apple-darwin15.5.0/6.1.0/f951
Reason: Incompatible library version: f951 requires version 14.0.0 or later, but libgmp.10.dylib provides version 13.0.0
Trace/BPT trap: 5
It says `gmp` in the correct path is not loaded since its version is `13.0.0`, but it is `14.0.0`! I know I have an older version `gmp` installed in another path, so after I remove its lib path from `DYLD_LIBRARY_PATH`, the problem is gone, but shouldn't `rpath` solve this problem?
Why `dyld` prints the correct path of `gmp`, but actually uses the wrong old? Is there any tricks in OS X? Thanks for help!
The version of `/opt/starman/software/gmp/6.1.0/0ec8ef118d09cb33f83559685d006f56a74f865c/lib/libgmp.10.dylib` is checked as
$ otool -l /opt/starman/software/gmp/6.1.0/0ec8ef118d09cb33f83559685d006f56a74f865c/lib/libgmp.10.dylib
...
cmdsize 120
name /opt/starman/software/gmp/6.1.0/0ec8ef118d09cb33f83559685d006f56a74f865c/lib/libgmp.10.dylib (offset 24)
time stamp 1 Thu Jan 1 08:00:01 1970
current version 14.0.0
compatibility version 14.0.0
...
And the older `gmp` is
$ otool -l /opt/software/gmp/6.0.0a/0/lib/libgmp.10.dylib
...
name @rpath/lib/libgmp.dylib (offset 24)
time stamp 1 Thu Jan 1 08:00:01 1970
current version 13.0.0
compatibility version 13.0.0
...