0

I am trying to cross-compile odas library for Raspberry PI. The code actually builds out of the box on the platform (either Raspberry PI, or Ubuntu box). However, when I am trying to cross compile the same code using this tool chain file (the whole thing started in the previous question):

# Cross-compilation system information
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

set(CMAKE_SYSTEM_PROCESSOR "arm")
# Specify the cross compiler
SET(CMAKE_C_COMPILER /home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/aarch64-rpi3-linux-gnu-gcc)
SET(CMAKE_CXX_COMPILER /home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/aarch64-rpi3-linux-gnu-g++)

# where is the target environment located
set(CMAKE_SYSROOT /home/raspberrypi/sysroot)
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})

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}")


# adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

I am getting a bunch of errors, like

[1/2] Linking C executable bin/odasserver
FAILED: bin/odasserver 
: && /home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/aarch64-rpi3-linux-gnu-gcc --sysroot=/home/raspberrypi/sysroot -g --sysroot=/home/raspberrypi/sysroot CMakeFiles/odasserver.dir/demo/odasserver/main.obj -o bin/odasserver  lib/libodas.so  -lfftw3f  -lasound  -lconfig  -lpulse-simple  -lpulse  -lm  -lpthread && :
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `__pthread_create'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `_dl_wait_lookup_done'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `_dl_init_static_tls'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `__pointer_chk_guard_local'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `_dl_stack_flags'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `_dl_pagesize'
collect2: error: ld returned 1 exit status

What could be possibly wrong here?

user2109066
  • 77
  • 1
  • 8
  • As you could find in the output, the linker command line contains **two** `--sysroot` specifications. This is because CMake automatically generates such option when `CMAKE_SYSROOT` variable is set. So your setting `CMAKE_EXE_LINKER_FLAGS` is not needed. Also, the directory specified in `CMAKE_SYSROOT` is automatically searched by `find_library` and other `find_*` commands. There is no reason to add this directory into `CMAKE_FIND_ROOT_PATH`. – Tsyvarev Feb 11 '23 at 06:55
  • According to [that question](https://stackoverflow.com/questions/5738000/undefined-reference-error-dl-stack-flags-with-gcc-and-pthreads), the reason of the error is that `pthread` library is linked **statically** but `libc` is linked dynamically. If your toolchain doesn't have the file `libpthread.so` but has its soversion variant like `libpthread.so.0` then you may create a *symlink* `libpthread.so` pointing to the soversion file. Like in [that comment](https://github.com/Azure/azure-iot-sdk-c/issues/1093#issuecomment-673339505). – Tsyvarev Feb 11 '23 at 07:15

1 Answers1

1

long story short - when creating sysroot folder on the host machine, I was using commands like

sudo rsync -avz rpi@192.168.1.205:/lib .
sudo rsync -avz rpi@192.168.1.205:/usr/include usr
sudo rsync -avz rpi@192.168.1.205:/usr/lib usr
sudo rsync -avz rpi@192.168.1.205:/usr/local usr

Apparently, on the target, some symbolic links were pointing outside of the current directory, for example

/usr/lib/aarch64-linux-gnu/libz.so -> /lib/aarch64-linux-gnu/libz.so.1.2.11

So needed to replace absolute symlinks with relative ones. Once this was done, then everything was fine and compiling, and running.

user2109066
  • 77
  • 1
  • 8