[CMake] rpath trouble on macosx: cmake 2.8.9 ignores INSTALL_NAME_DIR and INSTALL_RPATH?

Clinton Stimpson clinton at elemtech.com
Tue Sep 10 13:43:57 EDT 2013


On Tuesday, September 10, 2013 09:43:59 AM Dan Kegel wrote:
> Howdy!
> I'm using 2.8.9 installed on MacOSX using macports, and am trying to get
> a minimal example of rpath working.
> 
> Doing it without cmake works fine; see
>   http://kegel.com/macosx/rpath/demo.txt
> which just does
>   gcc -shared foo.c -install_name @rpath/foo.so -o foo.so
>   gcc -Wl,-rpath $OTHERDIR x.c foo.so
> and works fine.
> 
> But doing it with cmake eludes me.  Here's what I'm trying:
>   http://kegel.com/macosx/rpath/demo2.txt
>   http://kegel.com/macosx/rpath/CMakeLists.txt
> That CMakeLists.txt is just
> 
> cmake_minimum_required(VERSION 2.8)
> set(MYLIBDIR "${CMAKE_SOURCE_DIR}/otherdir")
> 
> add_library(foo SHARED foo.c)
> set_target_properties(foo PROPERTIES INSTALL_NAME_DIR "@rpath")
> 
> add_executable(x x.c)
> set_target_properties(
>     x
>     PROPERTIES
>     BUILD_WITH_INSTALL_RPATH 1
>     INSTALL_RPATH ${MYLIBDIR}
> )
> target_link_libraries(x foo)
> 
> When I run demo2.txt, it does
> 
> /usr/bin/gcc   -dynamiclib -Wl,-headerpad_max_install_names   -o
> libfoo.dylib -install_name /Users/dank/rpath/btmp/libfoo.dylib
> CMakeFiles/foo.dir/foo.c.o
> /usr/bin/gcc    -o CMakeFiles/x.dir/x.c.o   -c /Users/dank/rpath/x.c
> /usr/bin/gcc   -Wl,-search_paths_first
> -Wl,-headerpad_max_install_names   CMakeFiles/x.dir/x.c.o  -o x
> libfoo.dylib
> 
> and it appears to have totally ignored INSTALL_RPATH,
> so naturally running x fails with
> dyld: Library not loaded: /Users/dank/rpath/btmp/libfoo.dylib
> 
> Can I buy a clue, please?

Using the INSTALL_RPATH property doesn't work correctly unless you use CMake 
2.8.12 with the target property MACOSX_RPATH=1.

If you want it to work with the older version of CMake, then you need to set 
the -Wl,-rpath linker flag yourself using the LINK_FLAGS target property.

set_target_properties(x PROPERTIES LINK_FLAGS "-Wl,-rpath,${MYLIBDIR}")

You should still keep the BUILD_WITH_INSTALL_RPATH=1 if you want the 
INSTALL_NAME_DIR=@rpath to be applied at build time instead of waiting until 
install time.

Clint



More information about the CMake mailing list