You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Ubuntu ARM虚拟机为树莓派CMake交叉编译遇问题求助

树莓派交叉编译CMake配置错误排查

问题描述

我在M1 Mac上运行的Ubuntu 20.04 ARM服务器镜像虚拟机中,尝试为树莓派交叉编译项目,目前无法完成静态库测试文件编译,怀疑工具链存在问题。我使用的是ARM官网提供的aarch64-arm-none-linux-gnueabihf工具链,CMake配置如下:

set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_SYSTEM_PROCESSOR "armv8")

SET(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

set(CMAKE_C_COMPILER /opt/pi-cross-compile/gcc-arm-10.3-2021.07-aarch64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc-10.3.1)
set(CMAKE_CXX_COMPILER /opt/pi-cross-compile/gcc-arm-10.3-2021.07-aarch64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-g++)


SET(CMAKE_FIND_ROOT_PATH $ENV{RASPBIAN_ROOTFS})

运行CMake时出现如下错误:

- The C compiler identification is GNU 10.3.1
-- The CXX compiler identification is GNU 10.3.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /opt/pi-cross-compile/gcc-arm-10.3-2021.07-aarch64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc-10.3.1
-- Check for working C compiler: /opt/pi-cross-compile/gcc-arm-10.3-2021.07-aarch64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc-10.3.1 - broken
CMake Error at /usr/share/cmake-3.26/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/opt/pi-cross-compile/gcc-arm-10.3-2021.07-aarch64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc-10.3.1"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /home/parallels/kodlab_mjbots_sdk/build/CMakeFiles/CMakeScratch/TryCompile-FZkX11
    
    Run Build Command(s):/usr/bin/cmake -E env VERBOSE=1 /home/parallels/raspberrypi/rootfs/usr/bin/make -f Makefile cmTC_070a2/fast && /home/parallels/raspberrypi/rootfs/usr/bin/make: 1: Syntax error: word unexpected (expecting ")")
    
    
  
  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)


-- Configuring incomplete, errors occurred!

我尝试过完全禁用测试程序检查,但问题依旧。从错误信息来看,CMake报错树莓派rootfs中的make文件存在语法错误,似乎当前工具链无法正确读取该二进制文件。请问我哪里操作有误?是工具链选择错误,还是CMake配置不符合该工具链的交叉编译要求?


问题原因与修复方案

核心问题

错误提示里的/home/parallels/raspberrypi/rootfs/usr/bin/make是树莓派ARM架构的二进制文件,CMake错误地调用了rootfs里的ARM版make,而非宿主Ubuntu系统的make工具,导致执行时出现语法错误。

修复步骤

  1. 强制指定宿主系统的make工具
    在CMake配置中添加以下变量,强制使用宿主Ubuntu的make:

    set(CMAKE_MAKE_PROGRAM /usr/bin/make)
    
  2. 调整CMake路径查找优先级
    设置CMAKE_FIND_ROOT_PATH后,需要明确区分工具、库、头文件的查找路径:

    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_FIND_ROOT_PATH_MODE_PROGRAM NEVER:查找编译工具(如make、gcc)时,不使用rootfs路径,直接用宿主系统的
    • CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY:查找库文件时,仅在rootfs中搜索
    • CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY:查找头文件时,仅在rootfs中搜索
  3. 验证工具链与树莓派架构匹配性

    • 如果你的树莓派是32位系统(如Raspbian 32位),当前aarch64-arm-none-linux-gnueabihf工具链不匹配,需更换为32位ARM工具链arm-none-linux-gnueabihf
    • 如果是树莓派4/5的64位系统,需确保rootfs是64位版本,避免混合32位文件导致的兼容性问题
  4. 手动测试工具链可用性
    先单独验证工具链能否正常编译:

    # 编写简单测试文件test.c
    echo 'int main() { return 0; }' > test.c
    # 使用交叉编译器编译
    /opt/pi-cross-compile/gcc-arm-10.3-2021.07-aarch64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc -o test test.c
    # 检查输出文件架构
    file test
    

    输出应显示test: ELF 64-bit LSB executable, ARM aarch64,说明工具链正常工作。


内容的提问来源于stack exchange,提问作者Mason Mitchell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 03:03:09