Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change RPATH on OS X

I'm doing a simple installation from openkinect's website to use an xbox kinect. The problem is there is no OSX distribution and it is assumed homebrew will take care of the installation for you.

On the final step of installation I came upon a helpful clue as to what is wrong with my installation.

I am told by the site. "If you have problems with linking, you have to change the rpath of each libfreenect lib [using the following commands]:"

for i in /opt/local/lib/libfreenect*.dylib; do sudo install_name_tool -id $i $i; done
sudo install_name_tool -change libfreenect.0.2.dylib /opt/local/lib/libfreenect.0.2.dylib /opt/local/lib/libfreenect_sync.dylib
sudo install_name_tool -change libfreenect.0.2.dylib /opt/local/lib/libfreenect.0.2.dylib /opt/local/lib/libfreenect_cv.dylib
sudo install_name_tool -change libfreenect_sync.0.2.dylib /opt/local/lib/libfreenect_sync.0.2.dylib /opt/local/lib/libfreenect_cv.dylib
for i in glview regview hiview glpclview tiltdemo record cppview cvdemo; do sudo install_name_tool -change libfreenect.0.2.dylib /opt/local/lib/libfreenect.0.2.dylib /opt/local/bin/$i; sudo install_name_tool -change libfreenect_sync.0.2.dylib /opt/local/lib/libfreenect_sync.0.2.dylib /opt/local/bin/$i; done

My rpath is definitely set incorrectly. And this secondary message when building with CMake definitely confirms my doubts.

CMake Warning (dev):
  Policy CMP0042 is not set: MACOSX_RPATH is enabled by default.  Run "cmake
  --help-policy CMP0042" for policy details.  Use the cmake_policy command to
  set the policy and suppress this warning.

  MACOSX_RPATH is not specified for the following targets:

   fakenect
   freenect
   freenect_sync

The only issue I'm having is that the commands given to me up above do not function. Part of the reason is that my Mac places all my files under /usr/local/lib/ and NOT /opt/local/lib/. I copied over some of the files from usr to opt and it reduced the number of linking errors but I'm still left with a few. Again, they all have to do with libraries not being loaded into opt which makes this problem easier to solve.

Any help with how to properly execute the the first block of code above would be helpful!

sub-note (similar problems):

  • https://openkinect.org/wiki/Getting_Started#Manual_Build_under_OSX
  • CMake warnings under OS X: MACOSX_RPATH is not specified for the following targets
  • Can you please help me understand how Mach-O libraries work in Mac Os X?
like image 706
Max Avatar asked Oct 30 '16 03:10

Max


People also ask

What is macOS @rpath?

@rpath stands for Runpath Search Path. In the Xcode, it's set with LD_RUNPATH_SEARCH_PATH setting. In ld command tool it's set with -rpath parameter when linking. So it's a search path for the linker. Runtime Search Path instructs the dynamic linker to search a list of paths in order, to locate the dynamic library.

What is Dylib Mac OS X?

A DYLIB file is a Macintosh library that contains declarations and functions referenced by a macOS or iOS application. Programs load DYLIB files dynamically when needed, allowing for efficient memory usage. Apple Xcode users may reference DYLIB files while developing macOS programs.


1 Answers

Not sure if this is really helpful for your case. But I'm often able to resolve rpath-related problems with proper cmake arguments:

INSTALL_DIR="/where/it/goes"
cmake .. \
   -DCMAKE_BUILD_TYPE=Release \
   -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \
   -DCMAKE_MACOSX_RPATH=ON \
   -DCMAKE_INSTALL_RPATH="$INSTALL_DIR/lib" 

Every now and then (if for instance the project at hand doesn't set linker properties properly), I still have to engage install_name_tool after installation. For example:

install_name_tool -change \
    @rpath/QtWidgets.framework/Versions/5/QtWidgets \
    /opt/dev/lib/qt/QtWidgets.framework/Versions/5/QtWidgets \
    "$INSTALL_DIR/lib/libvtkRenderingQt-7.1.1.dylib"

Here, libvtkRenderingQt-7.1.1.dylib is a library that was created from a CMake project, built with the same CMake flags from above. For some reasons, libvtkRenderingQt could not find resources from the Qt framework, which was fixed by the above command. In pseudo syntax:

install_name_tool -change <old-path> <new-path> <file>

...where <old-path> is the one shown by otool -L <file>. Note that you could also change the rpath with install_name_tool in a similar way (but I haven't tried this out myself):

install_name_tool -rpath <old-path> <new-path> <file>

A last note: I prefer to install tools that I build myself to a local directory (e.g. /opt/dev, or some user directory) to not mess up other installations that have been acquired from other channels (like macport, brew, etc). This also prevents you from requiring sudo ops, as in your example.

like image 195
normanius Avatar answered Oct 02 '22 01:10

normanius