3

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

explo91
  • 530
  • 4
  • 11

3 Answers3

3

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)
benf
  • 915
  • 1
  • 9
  • 28
  • The idea can not be used in my case because my library is linked with other libs located in a non standard location. So, i need the rpath to be set to extra location in addition to /usr/local/lib. – explo91 Aug 17 '15 at 08:05
  • 1
    Since it is a non-standard location, you will have to set the path to the library *somewhere*. Setting LD_LIBRARY_PATH on the target system might be one solution. – benf Aug 17 '15 at 16:00
0

I am not sure which cross compile toolchain you are using.

You need to specify the C/CXX compilers, Linker etc. Along with that some of the important variables are CMAKE_FIND_ROOT_PATH_MODE_LIBRARY and CMAKE_FIND_ROOT_PATH_MODE_INCLUDE. If you set them to "ONLY", when you make calls to FindXXX(), the search happens only in the target root file system directory but not the build machine.

In my case I don't have to specify the sysroot as the cross compiler already knows that it's cross compiling and it also knows the location of the target root file system.

With this toolchain file, I just compile the sources without any additional flags, load the executable on the target and it runs fine picking up the *.so file directly from the right path.

Give it a try with this and let me know how it goes.

Here is my toolchain file:

set(ELDK_DIR /opt/eldk/ppc-v42-1)


set (CMAKE_C_COMPILER ${ELDK_DIR}/usr/bin/ppc_6xx-gcc)
set (CMAKE_CXX_COMPILER ${ELDK_DIR}/usr/bin/ppc_6xx-g++)
set (CMAKE_LINKER ${ELDK_DIR}/usr/bin/ppc_6xx-ld CACHE STRING "Set the cross-compiler tool LD" FORCE)
set (CMAKE_AR ${ELDK_DIR}/usr/bin/ppc_6xx-ar CACHE STRING "Set the cross-compiler tool AR" FORCE)
set (CMAKE_NM ${ELDK_DIR}/usr/bin/ppc_6xx-nm CACHE STRING "Set the cross-compiler tool NM" FORCE)
set (CMAKE_OBJCOPY ${ELDK_DIR}/usr/bin/ppc_6xx-objcopy CACHE STRING "Set the cross-compiler tool OBJCOPY" FORCE)
set (CMAKE_OBJDUMP ${ELDK_DIR}/usr/bin/ppc_6xx-objdump CACHE STRING "Set the cross-compiler tool OBJDUMP" FORCE)
set (CMAKE_RANLIB ${ELDK_DIR}/usr/bin/ppc_6xx-ranlib CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)
set (CMAKE_STRIP ${ELDK_DIR}/usr/bin/ppc_6xx-strip CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)

# Target environment
set (CMAKE_FIND_ROOT_PATH ${ELDK_DIR}/ppc_6xx)

# Don't search for programs in the host environment
set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Search for the libraries and headers in the target environment
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
leodotcloud
  • 1,830
  • 14
  • 15
  • My toolchain file defined teh compilers etc... My piece of code was shortten than my toolchain file. Cmake variables to control findXXX commands are correctly set as yours. I except that you don't link to a shared lib that is not in the default system folders of your sysroot and that is why your rpath is correctly set because not altered by CMake (your rpaths are system dirs) – explo91 May 26 '15 at 08:50
0

all you need is:

set(CMAKE_BUILD_RPATH "/my/libs/location")

specifying runtime path (RPATH) entries to add to binaries linked in the build tree (for platforms that support it). The entries will not be used for binaries in the install tree. See also the CMAKE_INSTALL_RPATH variable.

Alex B
  • 43
  • 5