gpt4 book ai didi

c++ - 交叉编译时 CMAKE 缺少 sysroot

转载 作者:可可西里 更新时间:2023-11-01 18:31:12 24 4
gpt4 key购买 nike

我在使用 CMAKE 设置交叉编译时遇到了一些问题。我使用的工具链是在 yocto 中创建的,它可以在 cmake 之外完美运行。

我按照教程设置了以下工具链文件:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_SYSTEM_PROCESSOR arm)

# specify the cross compiler
SET(tools /opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr)
SET(CMAKE_C_COMPILER ${tools}/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)
SET(CMAKE_CXX_COMPILER ${tools}/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)

# set sysroot
SET(CMAKE_SYSROOT /home/sifu/test-yocto/qemuarmdfs)
#SET(CMAKE_FIND_ROOT_PATH /home/sifu/test-yocto/qemuarm)

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

运行cmake时出现如下错误

The C compiler
"/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc"
is not able to compile a simple test program.

It fails with the following output:

Change Dir: /home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp

Run Build Command:/usr/bin/make "cmTryCompileExec4012536451/fast"

/usr/bin/make -f CMakeFiles/cmTryCompileExec4012536451.dir/build.make
CMakeFiles/cmTryCompileExec4012536451.dir/build

make[1]: Entering directory
`/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp'

/usr/bin/cmake -E cmake_progress_report
/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp/CMakeFiles
1

Building C object
CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o


/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
-O2 -pipe -g -feliminate-unused-debug-types -o
CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o -c
/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp/testCCompiler.c

Linking C executable cmTryCompileExec4012536451

/usr/bin/cmake -E cmake_link_script
CMakeFiles/cmTryCompileExec4012536451.dir/link.txt --verbose=1

/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
-O2 -pipe -g -feliminate-unused-debug-types -Wl,-O1 -Wl,--hash-style=gnu
-Wl,--as-needed CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o
-o cmTryCompileExec4012536451 -rdynamic

... (A lot of ld errors similar to the one below)

/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/4.9.1/ld:
cannot find crtn.o: No such file or directory

collect2: error: ld returned 1 exit status

make[1]: Leaving directory
`/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp'

如果我使用 --sysroot=/home/sifu/test-yocto/qemuarmdfs 手动运行上面日志中描述的 gcc 命令,它对我有用。当工具链文件中添加了 sysroot 的路径时,为什么 cmake 不使用 sysroot 标志。

最佳答案

这个问题我有两个解决方案:

  1. 我在工具链文件中使用以下代码:

    # compiler
    set(CMAKE_C_COMPILER /path/to/arm-none-linux-gnueabi-gcc)
    set(CMAKE_CXX_COMPILER /path/to/arm-none-linux-gnueabi-g++)
    # sysroot location
    set(MYSYSROOT /path/to/sysroots/cortexa7-vfp-neon-telechips-linux-gnueabi)
    # compiler/linker flags
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    # cmake built-in settings to use find_xxx() functions
    set(CMAKE_FIND_ROOT_PATH "${MYSYSROOT}")
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  2. 第二种解决方案是在工具链文件中使用 CMakeForceCompiler 包。它看起来不同,但我有相同的结果:

    # compiler
    include(CMakeForceCompiler)
    cmake_force_c_compiler(/path/to/arm-none-linux-gnueabi-gcc GNU)
    cmake_force_cxx_compiler(/path/to/arm-none-linux-gnueabi-g++ GNU)
    # sysroot location
    set(MYSYSROOT /path/to/sysroots/cortexa7-vfp-neon-telechips-linux-gnueabi)
    # compiler/linker flags
    add_definitions("--sysroot=${MYSYSROOT}")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    # cmake built-in settings to use find_xxx() functions
    set(CMAKE_FIND_ROOT_PATH ${MYSYSROOT})
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

顺便说一句,我不能使用 CMAKE_SYSROOT变量,因为它仅在 CMake v3.0 之后可用。

关于c++ - 交叉编译时 CMAKE 缺少 sysroot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com