Hello,

I am using ExternalProject to download and build a third-party library
(SDL) in my project. I am running into an issue on OSX where the RPATH
linker flags will not be added when my project is built and linked for the
first time. As a result the built executable will not have any RPATH paths
in it. However, when I rerun CMake or touch the main CMakeLists.txt file
and rebuild then the linker flags will be added appropriately.

I have tried explicitly setting the RPATH variables according to the "CMake
RPATH handling" page both for the default and "always full" scenarios but
that did not seem to change this behavior. Here is my current
CMakeLists.txt file:

cmake_minimum_required(VERSION 3.5)
project(shared_object)

set(CMAKE_MACOSX_RPATH TRUE)

set(LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/sdl/install)
set(LIB_DIR ${LIB_INSTALL_DIR}/lib)
set(LIB_PATH
${LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}SDL2${CMAKE_SHARED_LIBRARY_SUFFIX})

include(ExternalProject)
ExternalProject_Add(
    libsdl
    PREFIX ${CMAKE_BINARY_DIR}/sdl
    URL https://www.libsdl.org/release/SDL2-2.0.5.tar.gz
    URL_MD5 d4055424d556b4a908aa76fad63abd3c
    CMAKE_ARGS
        -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR}
        -DCMAKE_MACOSX_RPATH=${CMAKE_MACOSX_RPATH}
        -DSDL_STATIC=FALSE
    INSTALL_DIR ${LIB_INSTALL_DIR}
)

add_library(sdl SHARED IMPORTED GLOBAL)
set_target_properties(sdl PROPERTIES
    IMPORTED_LOCATION ${LIB_PATH}
)
add_dependencies(sdl libsdl)
add_executable(shared_object main.cpp)
target_link_libraries(shared_object sdl)

When I run CMake for the first time here are the contents of the
CMakeFiles/shared_object.dir/link.txt file:

/Applications/Xcode_6.4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
   -Wl,-search_paths_first -Wl,-headerpad_max_install_names
 CMakeFiles/shared_object.dir/main.cpp.o  -o shared_object
sdl/install/lib/libSDL2.dylib

Notice that there are no -Wl,-rpath linker flags. When I run `make` to
build the project there is no RPATH information in the built executable. I
checked this with `otool -l shared_object | grep -A2 RPATH`.

Now if I rerun CMake after everything has been built or if I touch the
CMakeLists.txt file before running `make` again then the contents of the
CMakeFiles/shared_object.dir/link.txt changes and the RPATH information is
linked into the built application. Here is the contents of
CMakeFiles/shared_object.dir/link.txt after I rerun CMake:

/Applications/Xcode_6.4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
   -Wl,-search_paths_first -Wl,-headerpad_max_install_names
 CMakeFiles/shared_object.dir/main.cpp.o  -o shared_object
-Wl,-rpath,/var/tmp/so/sdl/install/lib sdl/install/lib/libSDL2.dylib

After rebuliding the application it will contain the expected RPATH
information:

otool -l shared_object | grep -A2 RPATH
cmd LC_RPATH
cmdsize 40
path /var/tmp/so/sdl/install/lib (offset 12)

Is there something that I am configuring incorrectly about the external
project or imported library that causes it to not be known or added to the
linker flags the first time CMake is run?
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

Reply via email to