[CMake] getting the rpath right on osx

Andreas Pakulat apaku at gmx.de
Mon Nov 2 08:44:23 EST 2015


Hi,

On Mon, Nov 2, 2015 at 10:26 AM, Boudewijn Rempt <boud at valdyas.org> wrote:

> I checked the manual and the blog post about rpath on osx, but I'm still
> confused, and still not getting it right...
>
> I build and installed Qt 5.6 alpha like this:
>
> ./configure -prefix /Users/boudewijnrempt/kf5/i
>
> Then I made a small test project, consisting of nothing but a main that
> links to QtCore.
>
> If I build that with qmake, with this .pro file:
>
> QT       += core
> QT       -= gui
> TARGET = rpathqmake
> CONFIG   += console
> CONFIG   -= app_bundle
> TEMPLATE = app
> SOURCES += main.cpp
>
> The r-path is set:
>
> Boudewijns-Mac-mini:test boudewijnrempt$ otool -L rpathqmake
> rpathqmake:
>     @rpath/QtCore.framework/Versions/5/QtCore (compatibility version
> 5.6.0, current version 5.6.0)
>
> /System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration
> (compatibility version 1.0.0, current version 1.0.0)
>     /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
> (compatibility version 1.0.0, current version 275.0.0)
>     /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version
> 120.0.0)
>     /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current
> version 1213.0.0)
>
> Boudewijns-Mac-mini:test boudewijnrempt$ otool -L rpathqmake | grep -i
> rpath
> rpathqmake:
>     @rpath/QtCore.framework/Versions/5/QtCore (compatibility version
> 5.6.0, current version 5.6.0
>

Thats not the rpath in your executable, thats just the install name of the
QtCore library. And the install name indicates that you need to set an
rpath in your executable that points to the installation directory of Qt.
In order to see the rpath entries of your executable you'll need to check
for the LC_RPATH command in the output of this:

otool -l rpathqmake

It will show the absolute path to the Qt installation on your system.


> If I try a minimal cmakelists.txt, the rpath isn't set, I tried with and
> without all those RPATH related lines,
> they don't seem to make a difference. I'm using cmake 3.3.2.
>
> cmake_minimum_required(VERSION 2.8.12)
> cmake_policy(SET CMP0042 NEW)
> set(CMAKE_MACOSX_RPATH ON)
> SET(CMAKE_SKIP_BUILD_RPATH TRUE)
> SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
> SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
> set(REQUIRED_QT_VERSION 5.3.0)
> find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Core)
> add_executable(rpathcmake main.cpp)
> target_link_libraries(rpathcmake Qt5::Core)
> install(TARGETS rpathcmake DESTINATION /Users/boudewijnrempt/kf5/i/bin)
>
> Only adding something like this makes it work:
>
> set_target_properties(rpathcmake PROPERTIES INSTALL_RPATH
> "/Users/boudewijnrempt/kf5/i/lib")
>

I guess thats where your Qt is installed to? Then yes, that is exactly what
you want since thats where Qt is and the Qt libraries require an rpath to
be set to be found since 5.5 on OSX. You just don't want to hardcode this,
but rather calculate it off of the path of the QtCore library. Or even
better would be if the Qt5 cmake modules would provide some provision to
add the necessary linker commandline argument to inject the rpath during
linking into the executable. Thats how qmake makes things 'work out of the
box', it knows Qt has been built with the rpath-flag (default since 5.5)
and then adds something like -Wl,-rpath,<qtlibdir> to the linker
commandline of the generated Makefile.

I think the idea of using @rpath as install name of the Qt libraries is
geared towards the usecase of shipping Qt within the application bundle of
the application. In that case all you need is set the rpath
@executable_path/../Frameworks or so in the executable and thus the
app-bundle is relocatable. In order to get that with CMake you'll likely
need to use the BundleUtilities, though its been so long since I used those
I don't know if they can handle this scenario out of the box.

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20151102/126a41d6/attachment-0001.html>


More information about the CMake mailing list