How to set the runtime path (-rpath) of an executable with gcc under Mac OSX?

Found by experimentation, and inspecting the command lines generated by Xcode for a reference rpath demo project by Dave Driblin:

otool -L shows you the install name of the linked libraries. To get @rpath to work, you need to change the install name of the library:

$ gcc -dynamiclib blah.o -install_name @rpath/t/libblah.dylib -o libblah.dylib
$ mkdir t ; mv libblah.dylib t/
$ gcc main.c -lblah -L`pwd`/t -Xlinker -rpath -Xlinker `pwd`

Leave a Comment