Search code examples
cmakecross-compilingrpath

CMake cross compile target rpath


I am cross-compiling using CMake.

In my CMakeLists.txt (used for both compile and cross compile):

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
find_package(foo REQUIRED)
add_library(mylib SHARED ${SRCS})
target_link_libraries(mylib ${FOO_LIBRARIES)

In my toolchain.cmake:

set(CMAKE_CXX_FLAGS "... --sysroot=/path/to/sysroot/ ... ")
set(CMAKE_CXX_LINK_FLAGS "... --sysroot=/path/to/sysroot/ ... )
...
set(CMAKE_FIND_ROOT_PATH /path/to/sysroot)

Consider foo is located to /path/to/sysroot/usr/local/lib/foo.so, when i cross-compile the runtime path for mylib is /path/to/sysroot/usr/local/lib

I want that the runtime path is /usr/local/lib to reflect my target filesystem.

How can i do this without define a hard-coded CMAKE_INSTALL_RPATH variable in my CMakelists.txt ?

EDIT: I used /usr/local/lib for the example but foo lib are located to a specific folder that is not a part of the system dirs: /path/to/sysroot/usr/local/share/mypackage/lib/foo.so


Solution

  • Check out the wiki on CMake RPATH handling.

    By default, CMake compiles your executable with an RPATH pointing to the host-system library location (/crosssdk/sysroot/usr/lib/) and then (I believe) when installing (ie. make install) it edits the RPATH in the executable to replace it with the appropriate target RPATH (/usr/lib or wherever you've got it). I think the idea is that then you can make changes to the shared lib and execute the output on your host system without having to install both the shared lib and executable every time. In my case, my host is x86 and target is ARM, so I tell CMake to set the build RPATH the same as the install RPATH:

    set_target_properties(mytarget PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)