how to change RPATH on OS X
Asked Answered
B

1

12

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):

Berkey answered 30/10, 2016 at 3:42 Comment(0)
M
13

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.

Mantellone answered 7/11, 2017 at 16:17 Comment(1)
I also found this post very useful describing the usage of CMAKE_MACOSX_RPATH and CMAKE_INSTALL_RPATH.Mantellone

© 2022 - 2024 — McMap. All rights reserved.