0

trying to play around with odas library, using following CMakeLists.txt file to crosscompile for Raspberry PI platform:

make_minimum_required(VERSION 3.10)

# Set some basic project attributes
project (Audio_Sweep_Cross_CMake_04
    VERSION 0.1
    DESCRIPTION "Audio Sweep Project")

# Referencing ODAS here
include (CMakeListsOdas.cmake)

#Add base directory for includes (global)
include_directories(include)

set (SRC src)

# This project will output an executable file
add_executable(${PROJECT_NAME} ${SRC}/main.cpp )

# Include the configuration header in the build
SET(GCC_COVERAGE_LINK_FLAGS    "-lm -lpthread " )

set(ENV{PKG_CONFIG_PATH} "/sysroot/usr/lib/aarch64-linux-gnu/pkgconfig")
find_package(PkgConfig REQUIRED)
pkg_check_modules(PC_ALSA REQUIRED alsa)
pkg_check_modules(PC_FFTW3 REQUIRED fftw3f)
pkg_check_modules(PC_LIBCONFIG REQUIRED libconfig)
pkg_check_modules(PC_PULSEAUDIO REQUIRED libpulse-simple)

target_link_libraries(${PROJECT_NAME}
    odas
    ${PC_FFTW3_LIBRARIES}
    ${PC_ALSA_LIBRARIES}
    ${PC_LIBCONFIG_LIBRARIES}
    ${PC_PULSEAUDIO_LIBRARIES}
#     m
#     pthread
   )

SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")

This one compiles no problem. However, if I comment out

# SET(GCC_COVERAGE_LINK_FLAGS    "-lm -lpthread " )

and un-comment m and pthread in

target_link_libraries(${PROJECT_NAME}
    odas
    ${PC_FFTW3_LIBRARIES}
    ${PC_ALSA_LIBRARIES}
    ${PC_LIBCONFIG_LIBRARIES}
    ${PC_PULSEAUDIO_LIBRARIES}
    m
    pthread
   )

then, when i try to build the project, i am getting a bunch of errors:

**** Incremental Build of configuration Debug for project RPI_audio_sweep_04 ****
/usr/bin/ninja -j 8 all 
[1/1] Linking CXX executable Audio_Sweep_Cross_CMake_04
FAILED: Audio_Sweep_Cross_CMake_04 
: && /opt/x-tools/aarch64-rpi3-linux-gnu/bin/aarch64-rpi3-linux-gnu-g++ --sysroot=/sysroot -g --sysroot=/sysroot CMakeFiles/Audio_Sweep_Cross_CMake_04.dir/src/main.cpp.o -o Audio_Sweep_Cross_CMake_04  -Wl,-rpath,/RPI_audio_sweep_04/_build/Debug  libodas.so  -lfftw3f  -lasound  -lconfig  -lpulse-simple  -lpulse  -lm  -lpthread && :
/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: /sysroot/lib/aarch64-linux-gnu/libpthread.a(pthread_create.o): in function `allocate_stack':
./nptl/allocatestack.c:525: undefined reference to `_dl_stack_flags'
/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: ./nptl/allocatestack.c:647: undefined reference to `_dl_stack_flags'
/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: /sysroot/lib/aarch64-linux-gnu/libpthread.a(nptl-init.o): in function `__pthread_initialize_minimal_internal':
./nptl/nptl-init.c:335: undefined reference to `_dl_pagesize'
/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: ./nptl/nptl-init.c:344: undefined reference to `_dl_pagesize'
/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: ./nptl/nptl-init.c:360: undefined reference to `_dl_init_static_tls'
/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: ./nptl/nptl-init.c:362: undefined reference to `_dl_wait_lookup_done'
/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: /sysroot/lib/aarch64-linux-gnu/libpthread.a(nptl-init.o): in function `__pthread_get_minstack':
./nptl/nptl-init.c:393: undefined reference to `_dl_pagesize'
/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: /sysroot/lib/aarch64-linux-gnu/libpthread.a(unwind.o): in function `_jmpbuf_sp':
./nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
"/usr/bin/ninja -j 8 all" terminated with exit code 1. Build might be incomplete.

Not sure what is going on here.

user2109066
  • 77
  • 1
  • 8
  • 3
    The difference is that `CMAKE_EXE_LINKER_FLAGS` adds linker parameters to the beginning of the command line (**before** object files) but `target_link_libraries` adds parameters **after** object files. The order of libraries in the linker command line matters: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/24675715#24675715 – Tsyvarev Feb 09 '23 at 08:20
  • how **-pthread** can depend on anything in odas or my code? – user2109066 Feb 09 '23 at 08:57
  • 1
    A wild guess: It is not `pthread` library depends on your code, but it is your code uses some function, which could be **optionally** defined in `pthread` library. That is, if `pthread` library comes first, while no symbols is requested from it, the linker simply drops that library. But if the linker sees `pthread` after your objects, then linker finds out that pthread defines some symbols needed by your code. In that case the linker instantiates `pthread` library (and you get "undefined references" from it). Probably, your toolchain doesn't support `pthread` or incorrectly configured for it. – Tsyvarev Feb 09 '23 at 10:25
  • extended this question with the additional [one](https://stackoverflow.com/questions/75417779/cross-compillation-for-raspberry-pi) – user2109066 Feb 11 '23 at 03:14

0 Answers0