Search code examples
xcodemacoslinkershared-libraries

Remove default search path when linking new dylib.


I'm trying to link new dylib to my executable, but it link it with absolute path (/usr/local/lib/) how can I remove this default path from the used shared libraries ?

otool -L ../Build/Products/Debug/myexec

../Build/Products/Debug/myexec:
/usr/local/lib/libmylib.dylib (compatibility version 1.0.0, current version 1.0.0)

Solution

  • For macos and Mach-O binaries, install_name_tool command can change dynamic link libs.

    • -change old new binary option will change one or several libs' location.
    • -rpath old new binary will reset rpath to a new one.
    • -delete_rpath old binary will remove the current one.
    • -add_rpath new binary will add a new path.

    However, there may be a small trouble: it seems this cmd line tool can only set the binary's rpath, not the system rpath, so that /usr/local/lib cannot be removed by -delete_rpath option.

    In such case, the only option is -change old new.

    To check rpath:

    $ otool -l that_exec |grep -C 5 LC_RPATH
          cmd LC_DATA_IN_CODE
      cmdsize 16
      dataoff 13152
     datasize 0
    Load command 40
              cmd LC_RPATH
          cmdsize 32
             path /the_binarys_rpath/here (offset 12)