Adding a rpath item to the build tree generated executable

1.4k views Asked by At

I always run my executable in the build tree (I don't run it from a cmake "install"). A library, let's call it fruit, is built as a framework:

add_library( fruit SHARED ${FRUIT_SOURCES} )
set_target_properties( fruit PROPERTIES FRAMEWORK TRUE)
set_target_properties( fruit PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE
                             INSTALL_NAME_DIR "@rpath/Frameworks"   )

Now I want to set a custom rpath for main application (called executable) with cmake. I thought I could use the INSTALL_RPATH target property of executable to define my rpaths for the generated program, but this seems to only work for an installed executable (remember I always run my application in the cmake build folder):

# this rpath is not shown in the generated executable (otool -l -v executable):
set_target_properties( executable PROPERTIES INSTALL_RPATH "@executable_path/lib/" ) 

How define/add a rpath item to the program generated in the build tree?

PS. This library fruit is just an example of my actual problem. The library is created in a sub cmake project (a git submodule) which adds the BUILD_WITH_INSTALL_RPATH property to the library. But I have the possibility to change the code for this project. Is there a better way to let my executable work in the build tree?

1

There are 1 answers

0
telephone On

This is not an answer to the main question, but I figured out an answer to the post scriptum. This is a better solution than the one searced for in the main question, but I let that question persist.

set( CMAKE_MACOSX_RPATH TRUE )
set( CMAKE_SKIP_BUILD_RPATH  FALSE )
set( CMAKE_BUILD_WITH_INSTALL_RPATH FALSE ) 

################################################################################
# this is more interesting later when we implement install (i.e. creating a 
# bundle on macOS)in this CMakeLists!  
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# the RPATH to be used when installing, but only if it's not a system directory
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
   set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
endif("${isSystemDir}" STREQUAL "-1")
################################################################################

# add the dynamic library
add_library( fruit SHARED ${FRUIT_SOURCES} )
set_target_properties( fruit PROPERTIES FRAMEWORK TRUE)
# remove the properties INSTALL_NAME_DIR and BUILD_WITH_INSTALL_RPATH:
#set_target_properties( fruit PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE
#                             INSTALL_NAME_DIR "@rpath/Frameworks"   )

The code is taken from here. Also see this blog post.