ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

I came up with a temporary solution. My target device is a BeagleBone Black.

1) You need to create a simple toolchain.cmake file like the following one

# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

# Set the target architecture
set(CMAKE_LIBRARY_ARCHITECTURE arm-linux-gnueabihf)

# specify the cross compiler
SET(CMAKE_C_COMPILER   /usr/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++)

# where is the target environment 
SET(CMAKE_FIND_ROOT_PATH "/path/to/target-root") # no slash "/" at the end

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# Set compiler flag
SET(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}" )

2) Setup your catkin profile. Let's suppose you want to create a profile called "cross" and the toolchain.cmake file is located in src folder.

catkin config --profile cross --extend /path/to/target-root/opt/ros/kinetic
catkin config --profile cross --cmake-args  -DCMAKE_TOOLCHAIN_FILE=`pwd`/src/toolchain.cmake

It's also good to set an specific location for "build", "devel" and "install" folders.

3) In the CMakeLists.txt of your project, place the following code before you link your targets to catkin_LIBRARIES:

if(CMAKE_CROSSCOMPILING) 
    set(fixed_catkin_libraries)
    foreach (dir ${catkin_LIBRARIES})
        foreach(root_path ${CMAKE_FIND_ROOT_PATH})
            if(dir MATCHES "^${root_path}/") # assume CMAKE_FIND_ROOT_PATH has no "/" at the end
                list(APPEND fixed_catkin_libraries ${dir})
            else()
                list(APPEND fixed_catkin_libraries "${root_path}${dir}")
            endif()
        endforeach()
    endforeach()
    set(catkin_LIBRARIES ${fixed_catkin_libraries})
endif()

**4) Compile your project. If you get errors, try running touch src/toolchain.cmake

References:

[1] https://cmake.org/Wiki/CMake_Cross_Compiling

[2] https://github.com/AutoModelCar/catkin_ws_user

[3] https://github.com/ros/catkin/pull/811