Catkin and CMake integration question for cross-compilation ROS node - cmake

Trying to cross-compile a ros node which relays on some arm64 libraries located in a different path other than /usr/lib/arm64-
Error received is:
Errors << gps_confidence_module:make /gitlab/soads-ros/DAS/ros/logs/gps_confidence_module/build.make.013.log
make[2]: *** No rule to make target '/usr/lib/aarch64-linux-gnu/libconsole_bridge.so.0.4', needed by '/gitlab/soads-ros/DAS/ros/devel/.private/gps_confidence_module/lib/gps_confidence_module/gps_confidence_module'. Stop.
make[1]: *** [CMakeFiles/gps_confidence_module.dir/all] Error 2
make: *** [all] Error 2
cd /gitlab/soads-ros/DAS/ros/build/gps_confidence_module; catkin build --get-env gps_confidence_module | catkin env -si /usr/bin/make --jobserver-fds=6,7 -j; cd -
.....................................................................................................................................................................................
The error goes away when I add the path inside the CMakelist.txt with:
link_directories(
${PDK_PATH}/targetfs/usr/lib/aarch64-linux-gnu
)
Is there a way to make this link_directories global to my catkin environment?
trying to avoid to insert the link_directories code on every single CMakelists.txt of my ROS source code.

You should not do this in your CMakeLists.txt files yourself. This kind of info should go into your toolchain file (see https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html).
I haven't worked with your kind of setup, but setting CMAKE_SYSROOT should work in your case.
toolchain-arm64.cmake
set(PDK_PATH ...)
set(CMAKE_SYSTEM_NAME ...)
set(CMAKE_SYSTEM_PROCESSOR arm64)
set(CMAKE_CXX_COMPILER /path/to/c++/compiler)
set(CMAKE_C_COMPILER /path/to/c/compiler)
set(CMAKE_SYSROOT "${PDK_PATH}/targetfs")
...
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

Related

How to add Ziplib library in Clion on Ubuntu

I'm trying to add ZipLip into my project using Clion on ubuntu, but I have this output:
====================[ Build | TryZip | Debug ]==================================
/home/david/Snap/clion-2019.2.4/bin/cmake/linux/bin/cmake --build
/home/david/CLionProjects/TryZip/cmake-build-debug --target TryZip -- -j 2
[ 13%] Built target bzip2
[ 31%] Built target zlib
[ 83%] Built target lzma
[ 95%] Built target ZipLib
Scanning dependencies of target TryZip
[ 97%] Linking CXX executable ../bin/TryZip
/usr/bin/ld: cannot find -lExternalLibrary/ZipLib
collect2: error: ld returned 1 exit status
CMakeFiles/TryZip.dir/build.make:102: recipe for target '../bin/TryZip' failed
make[3]: *** [../bin/TryZip] Error 1
CMakeFiles/Makefile2:109: recipe for target 'CMakeFiles/TryZip.dir/all' failed
make[2]: *** [CMakeFiles/TryZip.dir/all] Error 2
CMakeFiles/Makefile2:116: recipe for target 'CMakeFiles/TryZip.dir/rule' failed
make[1]: *** [CMakeFiles/TryZip.dir/rule] Error 2
Makefile:131: recipe for target 'TryZip' failed
make: *** [TryZip] Error 2
This is my Cmakefile.txt
cmake_minimum_required(VERSION 3.15)
project(TryZip)
if(BOOST_FILESYSTEM)
include_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_LIB_DIR})
add_definitions(-DUSE_BOOST_FILESYSTEM)
else()
if(MSVC)
add_definitions(-DFILESYSTEM_EXPERIMENTAL)
endif()
endif()
if(BOOST_FILESYSTEM)
if(UNIX)
find_package(Boost COMPONENTS system filesystem REQUIRED)
target_link_libraries(${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY})
endif()
endif()
add_subdirectory(ExternalLibrary/ZipLib)
link_libraries(ExternalLibrary/ZipLib)
include_directories(ExternalLibrary/ZipLib)
set(CMAKE_CXX_STANDARD 17)
add_executable(TryZip main.cpp ExternalLibrary/ZipLib/ZipFile.cpp)
target_link_libraries(TryZip ZipLib)
Can someone help me to solve this please?
My ZipLib folder is in the same folder as my cmakefile.txt file.
The call to link_libraries() appears to accept the wrong arguments in this case. The link_libraries() command takes arguments of existing CMake targets, or library names. It is also redundant with your target_link_libraries() call, as this already links ZipLib to TryZip.
Try removing the call to link_libraries(), as this CMake function is deprecated and its use is highly discouraged. The include_directories() call is similarly deprecated, in favor of the target-specific command, so consider using target_include_directories() instead.
Assuming your added sub-directory ExternalLibrary/ZipLib contains an additional CMakeLists.txt file for configuring the ZipLib target, you should not need to add the ZipFile.cpp file again. If this file is already compiled in the sub-directory into the target ZipLib, you do not need to compile it again into TryZip.
add_subdirectory(ExternalLibrary/ZipLib)
set(CMAKE_CXX_STANDARD 17)
add_executable(TryZip main.cpp)
target_include_directories(TryZip PRIVATE ExternalLibrary/ZipLib)
target_link_libraries(TryZip PRIVATE ZipLib)
EDIT: Based on your feedback, it appears ZipLib also depends on pthread but somehow it is not getting linked correctly. You might try to add the following to your ExternalLibrary/ZipLib/CMakeLists.txt file (if it doesn't already exist), to utilize CMake's FindThreads module:
find_package(Threads REQUIRED)
...
target_link_libraries(ZipLib PUBLIC Threads::Threads)

CMakeLists.txt not in root directory, can't do an automatic build

I have an issue with a repository not having its CMakeLists in the root directory, namely https://github.com/lz4/lz4
The CMakeLists.txt is in the subfolder contrib/cmake_unofficial.
I already checked similar questions on SO (Is it possible to have cmake build file (CMakeLists.txt) not in root in CLion, cmake - CMakeLists.txt is not in root folder (but is included in source)), but they only provide alternatives, and not a solution applicable to my situation.
Heres the cmake module I came up with:
if(ENABLE_LZ4)
message(STATUS "Using LZ4.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_LZ4")
# Enable ExternalProject CMake module
include(ExternalProject)
set(LZ4_SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/lz4)
set(LZ4_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
# Download and install lz4
ExternalProject_Add(
lz4
GIT_REPOSITORY https://github.com/lz4/lz4.git
GIT_TAG dev
SOURCE_DIR ${LZ4_SOURCE_DIR}
BINARY_DIR ${LZ4_BINARY_DIR}
INSTALL_COMMAND ""
CMAKE_ARGS
${LZ4_SOURCE_DIR}/contrib/cmake_unofficial
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
)
# Get lz4 source and binary directories from CMake project
ExternalProject_Get_Property(lz4 source_dir binary_dir)
# Create a liblz4 target to be used as a dependency by the program
add_library(liblz4 IMPORTED SHARED GLOBAL)
add_dependencies(liblz4 lz4)
include_directories(
${LZ4_SOURCE_DIR}/lib
)
set(LZ4_LIB ${LZ4_BINARY_DIR}/liblz4.so)
else()
message(STATUS "Not using LZ4.")
set(LZ4_LIB "")
endif()
Here the complete error output:
[ 0%] Performing update step for 'lz4'
Current branch dev is up to date.
[ 1%] Performing configure step for 'lz4'
CMake Error: The source directory "/****/build/lz4" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
CMakeFiles/lz4.dir/build.make:105: recipe for target 'lz4-prefix/src/lz4-stamp/lz4-configure' failed
make[2]: *** [lz4-prefix/src/lz4-stamp/lz4-configure] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/lz4.dir/all' failed
make[1]: *** [CMakeFiles/lz4.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2
I tried adding the path 'contrib/cmake_unofficial' to the CMAKE_ARGS variable (as seen in the module above), but it does not work (seems to be ignored?).
I also tried using PATCH_COMMAND to copy the CMakeLists.txt to the root before the build starts, but the relative paths of the file get messed up.
In other words, i need the cmake command to be called to build the library to be : cmake contrib/cmake_unofficial.
I also tried using CONFIGURE_COMMAND for this, but keep getting a file not found error for some reason (even though the path is correct).
The module has some other issues too, but I'm only interested in the non-root CMakeLists.
Thanks in advance!
ExternalProject separates download and source directories:
DOWNLOAD_DIR - a directory where downloading step is performed
SOURCE_DIR - a directory used as a source one when configuration step is performed
When use git for extract the project, note that git clone is called from the download directory, where it creates new directory with a project sources.
set(LZ4_DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}/lz4)
# Set a source dir based on the download one.
set(LZ4_SOURCE_DIR ${LZ4_DOWNLOAD_DIR}/lz4/contrib/cmake_unofficial)
set(LZ4_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
# Download and install lz4
ExternalProject_Add(
lz4
DOWNLOAD_DIR ${LZ4_DOWNLOAD_DIR} # Set download directory explicitely
GIT_REPOSITORY https://github.com/lz4/lz4.git
GIT_TAG dev
SOURCE_DIR ${LZ4_SOURCE_DIR}
BINARY_DIR ${LZ4_BINARY_DIR}
...
)
Probably not the correct way to do it, but this seems to work for me.
I used the CONFIGURE_COMMAND to call cmake on the correct directory.
Then use BUILD_COMMAND to call make
So essentially, it breaks down to this:
ExternalProject_Add(
lz4
GIT_REPOSITORY https://github.com/lz4/lz4.git
GIT_TAG dev
SOURCE_DIR ${LZ4_SOURCE_DIR}
BINARY_DIR ${LZ4_BINARY_DIR}
CONFIGURE_COMMAND cmake ${LZ4_SOURCE_DIR}/contrib/cmake_unofficial
BUILD_COMMAND make
INSTALL_COMMAND ""
CMAKE_ARGS
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
)

CMAKE and cross compiling

I have to change sysroot for my cmake. There is rootfs located on my disk in /u02/rootfs.
I created toolchain_rootfs file and added this config into it:
SET(CMAKE_INSTALL_PREFIX "/u02/rootfs")
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSROOT ${CMAKE_INSTALL_PREFIX})
#set(CMAKE_STAGING_PREFIX /home/devel/stage)
set(CMAKE_FIND_ROOT_PATH ${CMAKE_INSTALL_PREFIX})
set(CMAKE_C_COMPILER "${CMAKE_INSTALL_PREFIX}/usr/bin/gcc")
set(CMAKE_CXX_COMPILER "${CMAKE_INSTALL_PREFIX}/usr/bin/c++")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
###
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/usr/lib")
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/usr/lib")
After that I run cmake with option:
cmake ../myproject/ -DCMAKE_TOOLCHAIN_FILE=/u02/myproject/toolchain_rootfs
And got error like this:
------------ /u02/rootfs/usr/lib
------------ /u02/rootfs/usr/lib
-- The C compiler identification is unknown
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /u02/rootfs/usr/bin/gcc
------------ /u02/rootfs/usr/lib
-- Check for working C compiler: /u02/rootfs/usr/bin/gcc -- broken
CMake Error at /usr/share/cmake-3.5/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "/u02/rootfs/usr/bin/gcc" is not able to
compile a simple test program.
It fails with the following output:
Change Dir: /u02/myproject/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_0e039/fast"
/usr/bin/make -f CMakeFiles/cmTC_0e039.dir/build.make
CMakeFiles/cmTC_0e039.dir/build
make[1]: Entering directory '/u02/myproject_build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_0e039.dir/testCCompiler.c.o
/u02/rootfs/usr/bin/gcc -o
CMakeFiles/cmTC_0e039.dir/testCCompiler.c.o -c
/u02/myproject_build/CMakeFiles/CMakeTmp/testCCompiler.c
/u02/rootfs/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/cc1:
error while loading shared libraries: libisl.so.10: cannot open shared
object file: No such file or directory
Could you help me please solve this error. I went through cmake manual, set RPATH, but it doesn't work to me. What is wrong?
UDP1:
I've tried to use sysroot, without toolchain like:
SET(CMAKE_INSTALL_PREFIX "/u02/rootfs")
set(CMAKE_SYSROOT ${CMAKE_INSTALL_PREFIX})
set(CMAKE_FIND_ROOT_PATH ${CMAKE_INSTALL_PREFIX})
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/usr/lib/x86_64-linux-gnu")
CMAKE was passed, but MAKE was failed. I got error:
/u02/rootfs/usr/bin/ar: error while loading shared libraries: libbfd-2.25-system.so: cannot open shared object file: No such file or directory
rootfs has this library.
How to specify PATH to LIBs in rootfs(sysroot)?

Target file fails during INSTALL stage with CMake

I have a CMakeLists.txt file that is building a shared lib as en ExternalProject and this lib needs to be installed in a specific location so that it can be picked up by the main project at build time. I am using the following line to perform the install process:
install(FILES $<TARGET_FILE:of_shared> DESTINATION oF/lib)
However, this fails:
Install the project...
-- Install configuration: ""
CMake Error at cmake_install.cmake:31 (FILE):
file INSTALL cannot find
"/Users/me/packages/builds/x86_64/of-0.9.3-osx-release/$<TARGET_FILE:of_shared>".
make[3]: *** [install] Error 1
make[2]: *** [of_shared-prefix/src/of_shared-stamp/of_shared-install] Error 2
make[1]: *** [CMakeFiles/of_shared.dir/all] Error 2
make: *** [all] Error 2
This might be a compatibility bug of some sort, maybe between CMake versions. I am using CMake 2.8.12.
These are the related CMake lines:
add_library(core OBJECT ${OF_SOURCE_FILES})
add_library(of_shared SHARED $<TARGET_OBJECTS:core>)
set_target_properties(of_shared PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set_target_properties(of_shared PROPERTIES OUTPUT_NAME openFrameworks)
target_link_libraries(of_shared ${OF_CORE_FRAMEWORKS} ${OF_ADDON_FRAMEWORKS} ${OF_CORE_LIBS} ${OF_ADDON_LIBS})
install(FILES $<TARGET_FILE:of_shared> DESTINATION oF/lib)
install(FILES libs/fmodex/lib/osx/libfmodex.dylib DESTINATION oF/lib)
install(DIRECTORY libs/glut/lib/osx/GLUT.framework DESTINATION .)
Obviously that bit with the $<TARGET_FILE:of_shared> is failing. Apart from that, everything is working. I can see the shared lib being created.
I have found the solution myself. Instead of using:
install(FILES $<TARGET_FILE:of_shared> DESTINATION openFrameworks/lib)
One should use:
install(TARGETS of_shared LIBRARY DESTINATION openFrameworks/lib)
In that case of_shared is the name of the library.

How to pass a compile flag to cmake initial compiler test?

I'm trying to use CMake for building a project which uses the MSPGCC cross-compiler for a MSP430 microcontroller. To successfully compile any simple program with it, we need to pass a compile flag indicating the target processor, or else it fails like this:
$ msp430-gcc -o test test.c
In file included from test.c:1:0:
/usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/include/msp430.h:813:2: warning: #warning Unable to identify and include MCU header, use -mmcu=MCU [-Wcpp]
/usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld: cannot open linker script file memory.x: No such file or directory
collect2: ld returned 1 exit status
Hence, if I indicate the processor using the -mmcu switch it works fine. The problem is, although I'm already specifying this in my CMakeLists.txt file:
cmake_minimum_required (VERSION 2.6)
project (test-project C)
set (SOURCES
test.c
)
add_executable (test-project ${SOURCES})
set (CPU_FLAG "-mmcu=msp430f148")
set (CMAKE_C_FLAGS ${CPU_FLAG})
set (CMAKE_EXE_LINKER_FLAGS ${CPU_FLAG})
CMake complains the compiler failed the test to compile a simple program, which I bet is happening because it is probably not using the -mmcu switch (note the message about not being able to open linker script file memory.x):
$ cd ~/git/test-project
$ mkdir build
$ cd build
$ cmake -DCMAKE_TOOLCHAIN_FILE=../msp430.cmake ..
-- The C compiler identification is GNU 4.6.3
-- Check for working C compiler: /usr/local/bin/msp430-gcc
-- Check for working C compiler: /usr/local/bin/msp430-gcc -- broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
The C compiler "/usr/local/bin/msp430-gcc" is not able to compile a simple
test program.
It fails with the following output:
Change Dir: /home/claudio/git/test-project/build/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/gmake "cmTryCompileExec2889462763/fast"
/usr/bin/gmake -f CMakeFiles/cmTryCompileExec2889462763.dir/build.make
CMakeFiles/cmTryCompileExec2889462763.dir/build
gmake[1]: Entering directory
`/home/claudio/git/test-project/build/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report
/home/claudio/git/test-project/build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object
CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o
/usr/local/bin/msp430-gcc -o
CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o -c
/home/claudio/git/test-project/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTryCompileExec2889462763
/usr/bin/cmake -E cmake_link_script
CMakeFiles/cmTryCompileExec2889462763.dir/link.txt --verbose=1
/usr/local/bin/msp430-gcc
CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o -o
cmTryCompileExec2889462763 -rdynamic
/usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld: cannot open
linker script file memory.x: No such file or directory
collect2: ld returned 1 exit status
gmake[1]: Leaving directory
`/home/claudio/git/test-project/build/CMakeFiles/CMakeTmp'
gmake[1]: *** [cmTryCompileExec2889462763] Error 1
gmake: *** [cmTryCompileExec2889462763/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)
-- Configuring incomplete, errors occurred!
Just for the record, my toolchain file is as follows, and my PATH variable allows it to find the compiler binaries at /usr/local/bin:
# the name of the target operating system
#SET(CMAKE_SYSTEM_NAME Linux)
# which C and C++ compiler to use
SET(CMAKE_C_COMPILER msp430-gcc)
SET(CMAKE_CXX_COMPILER msp430-g++)
# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH /usr/local/msp430)
# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
All that said, can anyone tell me how to check which compile flags CMake is using to carry the compiler test, and how can we pass custom flags (like -mmcu, for instance) so it doesn't fail it?
According to the Docs:
http://www.cmake.org/Wiki/CMake_Cross_Compiling#The_toolchain_file
you should use the CMakeForceCompiler thing
INCLUDE(CMakeForceCompiler)
# this one is important
SET(CMAKE_SYSTEM_NAME eCos)
# specify the cross compiler
CMAKE_FORCE_C_COMPILER(arm-elf-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(arm-elf-g++ GNU)
# where is the target environment
SET(CMAKE_FIND_ROOT_PATH /home/alex/src/ecos/install )
# 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)
(copy&paste from the docs)
Using it fine here for my MSP430 too
The other answer here is outdated.
The toolchain file should include the required flags in the appropriate CMAKE_<KIND>_FLAGS(_<CONFIG>)_INIT variables, like so:
set(CMAKE_C_FLAGS_INIT "-mmcu=msp430f148")
set(CMAKE_CXX_FLAGS_INIT "-mmcu=msp430f148")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-mmcu=msp430f148")
CMake's compiler detection routines will use these flags when compiling a test executable. The full list is available in the CMake variables documentation, but the full list (at time of writing) is:
CMAKE_<LANG>_FLAGS_<CONFIG>_INIT
CMAKE_<LANG>_FLAGS_INIT
CMAKE_EXE_LINKER_FLAGS_<CONFIG>_INIT
CMAKE_EXE_LINKER_FLAGS_INIT
CMAKE_MODULE_LINKER_FLAGS_<CONFIG>_INIT
CMAKE_MODULE_LINKER_FLAGS_INIT
CMAKE_SHARED_LINKER_FLAGS_<CONFIG>_INIT
CMAKE_SHARED_LINKER_FLAGS_INIT
CMAKE_STATIC_LINKER_FLAGS_<CONFIG>_INIT
CMAKE_STATIC_LINKER_FLAGS_INIT
Where <CONFIG> is any ALL CAPS config name including, but not limited to, DEBUG, RELWITHDEBINFO, MINSIZEREL, and RELEASE. The non-<CONFIG> variants apply to all configurations and are augmented by the <CONFIG> variants.
And, where <LANG> is any of the languages known to enable_language(), currently including CXX, C, CUDA, OBJC, OBJCXX, Fortran, HIP, ISPC, and ASM. The current list may be found in the enable_language documentation.