0

I want to cross-compile c++ code for my raspberry-pi on my notebook (Ubuntu 18.04) and I spent hours and it still doesn't work. I followed this little tutorial and it's working. I want to link against the eclipse paho-mqtt-c++ lib, which I installed on my raspberry. I did rsync with my raspberry as in the tutorial, I now have a rootfs folder and there under rootfs/usr/local/include/ the header files for using the lib are stored and the lib itself is under rootfs/usr/local/lib.

My toolchain cmake file contains the following:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_COMPILER   $ENV{HOME}/applications/raspberry-tools/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/applications/raspberry-tools/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++)
#SET(CMAKE_SYSROOT /home/manuelhoffmann/applications/raspberry-tools/rootfs)
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/applications/raspberry-tools/rootfs)
#SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
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_LIBRARY_PATH /home/manuelhoffmann/applications/raspberry-tools/rootfs/usr/local/lib)
include_directories(${CMAKE_FIND_ROOT_PATH}/usr/local/include)
#INCLUDE_DIRECTORIES($ENV{HOME}/applications/raspberry-tools/rootfs/usr/local/include)
#link_directories(${CMAKE_FIND_ROOT_PATH}/usr/local/lib)
#find_library(${CMAKE_FIND_ROOT_PATH}/usr/local/lib)

add_definitions(-Wall -std=c11)

The include_directories was necessary so that it finds the headers and now it is compiling fine. But it still has a problem when linking, the output is:

raspberry-tools/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++  -pthread   CMakeFiles/kg_iot_gateway.dir/gateway.cpp.o CMakeFiles/kg_iot_gateway.dir/MQTTClient.cpp.o CMakeFiles/kg_iot_gateway.dir/socket.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MessageGWInfo.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MessagePublish.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MQTTSNFlags.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MQTTSNMessage.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MQTTSNMessageHeader.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MQTTSNMessageVariablePart.cpp.o CMakeFiles/kg_iot_gateway.dir/main.cpp.o  -o kg_iot_gateway -lpaho-mqttpp3 
/applications/raspberry-tools/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.5.0/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lpaho-mqttpp3
collect2: error: ld returned 1 exit status
CMakeFiles/kg_iot_gateway.dir/build.make:218: recipe for target 'kg_iot_gateway' failed
make[2]: *** [kg_iot_gateway] Error 1
make[2]: Leaving directory '/kg-iot-gateway/build'
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/kg_iot_gateway.dir/all' failed
make[1]: *** [CMakeFiles/kg_iot_gateway.dir/all] Error 2
make[1]: Leaving directory '/kg-iot-gateway/build'
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

All commented lines didn't work or bring the desired result.

So how can I tell cmake to look for libs also in rootfs/usr/local/lib? What am I doing wrong?

Thanks for helping!

  • Why are some parts commented out? – user7860670 Aug 15 '19 at 10:15
  • These are the lines which I tried but which are not working or bringing any change at the end. – Manuel Hoffmann Aug 15 '19 at 10:17
  • @Manuel How did you setup your cross compiling environment and tool chain at your machine? – πάντα ῥεῖ Aug 15 '19 at 10:29
  • Is the library in subdir of `rootfs/usr/local/lib`? Link_directories does not search recursively. – super Aug 15 '19 at 10:41
  • What is meant by cross-compiling environment? My toolchain is the linaro gcc toolchain, gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf, and I use them in the toolchain cmake file by calling the gcc and g++ compiler. The library is direclty located in rootfs/usr/local/lib. – Manuel Hoffmann Aug 15 '19 at 10:48
  • I would suggest installing distcc on your Raspberry PI and telling it to use your cross compiler. Then you can build on your RaspberryPI but use the power of your x86_64 to do the compile part. You can even do that under rootfs/ and chroot into rootfs/ if you set up qemu-user-arm in /proc/sys/fs/binfmt_misc as describe in for example https://ownyourbits.com/2018/06/13/transparently-running-binaries-from-any-architecture-in-linux-with-qemu-and-binfmt_misc/. – Goswin von Brederlow Aug 15 '19 at 13:48
  • As you attempt to add option `--sysroot=${CMAKE_FIND_ROOT_PATH}`, it seems that your *root path* is actually a **sysroot**. CMake has a special variable for that purpose: `CMAKE_SYSROOT`. With setting of this variable, CMake automatically adds `--sysroot` option when needed. BTW, this variable is noted in some answers to the referenced question. E.g. by this one: https://stackoverflow.com/a/35200622/3440745. – Tsyvarev Aug 15 '19 at 19:24

0 Answers0