使用CMake和SYSROOT路径进行交叉编译的问题

时间:2019-06-03 17:11:22

标签: c++ cmake arm cross-compiling linaro

在使用CMake(用于手臂的交叉编译)生成可执行文件并在工具链文件中设置CMAKE_SYSROOT变量时,链接器不会自动查找其他库(已明确链接到)依赖的库(例如'z'和' glib')。当不使用工具链文件(因此不进行交叉编译)时,不需要在target_link_libraries()中明确提及这些文件,但是当使用工具链文件时,它们会这样做,否则始终会出现找不到链接器的错误。我想知道为什么会这样。

我的环境的简要说明:

  • 使用Linux Ubuntu 16编译运行Ubuntu 18的arm目标
  • 从目标压缩了根文件系统,并在开发系统上解压缩了
  • Linaro交叉构建工具链(7.4.1)
  • 用于cmake的工具链文件,该文件定义:CMAKE_SYSROOT,CMAKE_CXX_COMPILER,CMAKE_C_COMPILER和CMAKE_AR_COMPILER
  • 编译器路径:/home/brian/workspace/ddpx_xcompile/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu
  • 目标根文件系统的路径:/ home / brian / workspace / ddpx_xcompile / ddpx-rootfs-05172019

我尝试了以下多种组合:设置CMAKE_LIBRARY_PATH,link_directories等。但是,唯一成功的选项似乎是在target_link_libraries()命令中添加以下内容:'z dl glib-2.0 pcre')

下面是错误消息的示例:     / aarch64-linux-gnu / bin / ld:警告:/lib/libopencv_core.so.4.0.1所需的libdl.so.2,未找到(尝试使用-rpath或-rpath-link)

然后是指示库中未定义函数的简短列表。此错误是针对libdl.so.2找不到的,还有其他错误。

我的错误输出:

/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

我的Cmake工具链文件:

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

0 个答案:

没有答案