When using CMake for crosscompiling ARM executables and setting the CMAKE_SYSROOT variable in a toolchain file, the linker does not automatically find libraries which other libraries (which were explicitly linked to) depend on, such as z
and glib
. When not using a toolchain file (and thus not cross-compiling) these do not need to be explicitly mentioned in target_link_libraries(), however when using the toolchain file they do or else there is always a linker error that they could not be found. I would like to understand why this is.
Brief description of my environment:
- Using Linux Ubuntu 16 to compile for arm target running Ubuntu 18
- Root file-system zipped from target and unzipped on development system
- Linaro cross-build toolchain (7.4.1)
- a toolchain file for cmake, which defines: CMAKE_SYSROOT, CMAKE_CXX_COMPILER, CMAKE_C_COMPILER and CMAKE_AR_COMPILER
- Path to compiler: /home/brian/workspace/ddpx_xcompile/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu
- Path to target root file system: /home/brian/workspace/ddpx_xcompile/ddpx-rootfs-05172019
I've tried many combinations of: setting the CMAKE_LIBRARY_PATH, link_directories etc. However, the only successful option seems to be adding the following inside the target_link_libraries() command: z dl glib-2.0 pcre
)
Below is an example of the error message:
<path-to-toolchain>/aarch64-linux-gnu/bin/ld:
warning: libdl.so.2, needed by <path-to-sharedlib>/lib/libopencv_core.so.4.0.1, not found (try using -rpath or -rpath-link)
And this is followed by a short list of undefined functions which are within the indicated library. This error is for libdl.so.2 not found
, there are others.
My error output:
/home/brian/workspace/ddpx_xcompile/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.4.1/../../../../aarch64-linux-gnu/bin/ld: warning: libdl.so.2, needed by /home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1, not found (try using -rpath or -rpath-link)
/home/brian/workspace/ddpx_xcompile/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.4.1/../../../../aarch64-linux-gnu/bin/ld: warning: libpthread.so.0, needed by /home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1, not found (try using -rpath or -rpath-link)
/home/brian/workspace/ddpx_xcompile/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.4.1/../../../../aarch64-linux-gnu/bin/ld: warning: libz.so.1, needed by /home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1, not found (try using -rpath or -rpath-link)
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `pthread_key_create@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `gzeof'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `pthread_setspecific@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `pthread_join@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `pthread_getspecific@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `pthread_key_delete@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `pthread_create@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `dlclose@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `gzrewind'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `dlopen@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `dladdr@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `gzopen'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `dlsym@GLIBC_2.17'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `gzclose'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `gzgets'
/home/brian/workspace/test/crossbuild/test_rootfs/ddpx-rootfs-05172019/home/nvidia/3rd_party_libs/opencv/install-cxx11/lib/libopencv_core.so.4.0.1: undefined reference to `gzputs'
collect2: error: ld returned 1 exit status
CMakeFiles/imshow.dir/build.make:95: recipe for target 'imshow' failed
make[2]: *** [imshow] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/imshow.dir/all' failed
make[1]: *** [CMakeFiles/imshow.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
My Cmake Toolchain file:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(TARGET_FS "/home/brian/workspace/ddpx_xcompile/ddpx-rootfs-05172019" CACHE STRING "test" FORCE)
set(CMAKE_SYSROOT ${TARGET_FS})
#Tried with and without the ${TARGET_FS} prefix, also tried with all uncommented and all commented
#link_directories(/lib/aarch64-linux-gnu)
#link_directories(/usr/lib)
#link_directories(/usr/lib/aarch64-linux-gnu)
#link_directories(${TARGET_FS}/lib/aarch64-linux-gnu)
#link_directories(${TARGET_FS}/usr/lib)
#link_directories(${TARGET_FS}/usr/lib/aarch64-linux-gnu)
#Did not try too much with these
#set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})
#set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${CMAKE_SYSROOT}/lib/aarch64-linux-gnu)
#Tried with two different compiler versions
set(CROSSBIN /home/brian/workspace/ddpx_xcompile/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu)
#set(CROSSBIN /home/brian/workspace/ddpx_xcompile/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu)
set(CMAKE_CXX_COMPILER "${CROSSBIN}/bin/aarch64-linux-gnu-g++")
set(CMAKE_C_COMPILER "${CROSSBIN}/bin/aarch64-linux-gnu-gcc")
set(CMAKE_AR_COMPILER "${CROSSBIN}/bin/aarch64-linux-gnu-ar")
#This was also an experiment, no change
#set(CMAKE_PREFIX_PATH ${TARGET_FS})
#list(APPEND CMAKE_PREFIX_PATH ${TARGET_FS}/lib/aarch64-linux-gnu)
#list(APPEND CMAKE_PREFIX_PATH /lib/aarch64-linux-gnu)
#list(APPEND CMAKE_LIBRARY_PATH "/lib/aarch64-linux-gnu")
#list(APPEND CMAKE_LIBRARY_PATH "/usr/lib/aarch64-linux-gnu")
#Tried with the 4 below uncommented or just commented
#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)
message(STATUS "CMAKE_LIBRARY_PATH: ${CMAKE_LIBRARY_PATH}")