首页 文章

如何在我的Linux主机上安装Raspberry Pi交叉编译器?

提问于
浏览
97

我正在尝试为我的Ubuntu机器上的Raspberry Pi进行交叉编译 .

在我最初的尝试中,我使用的是arm-linux-gnueabi编译器,它可以在Ubuntu repo中找到 . 我有这个工作 . 我能够构建所有依赖项并在我的cmake项目中使用交叉编译器 .

但是,我相信我应该使用hf版本,所以我切换到arm-linux-gnueabihf . 然后我意识到这不适用于Raspberry Pi,因为它是armv6 .

经过一些谷歌搜索后,我找到了pre-built toolchain from GitHub .

我下载了工具链,但我真的不明白如何“安装”它 . 我将文件解压缩到我的主目录 . 目录结构如下所示:

/gcc-linearo-arm-linux-gnueabihf-raspbian
    /arm-linux-gnueabihf
        /bin
            (contains g++, gcc, etc)
        /lib
            (contains libstdc++ library)
    /bin
        (contains arm-linux-gnueabihf-g++, arm-linux-gnueabihf-...)
    /lib
        (gcc lib stuff)

如果我将目录更改为INNER bin文件夹,我可以从终端编译测试程序,没有任何问题 .

~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/
arm-linux-gnueabihf/bin$ g++ test.cpp -o test

然后我尝试在OUTER bin文件夹中编译测试程序,该文件夹包含工具的前缀版本 .

~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin$ 
 arm-linux-gnueabihf-g++ test.cpp -o test

但是,当我尝试使用编译器时(从内部bin目录之外),它无法找到工具链附带的libstdc共享库:

arm-linux-gnueabihf-gcc: error while loading shared libraries: 
libstdc++.so.6: cannot open shared object file: No such file or directory.

此外,我希望能够使用编译器而无需导航到bin目录 . 所以我尝试添加OUTER bin目录(因为我想要前缀版本)和两个lib目录到我的PATH:

export PATH=$PATH:~/tools/.../bin
export PATH=$PATH:~/tools/.../lib
export PATH=$PATH:~/tools/.../.../lib

但是,这会导致相同的错误 . 我应该如何“安装”工具链,以便我可以在任何地方使用工具链,就像我在使用Ubuntu repo中的交叉编译器时一样?

8 回答

  • 2

    我无法使用git://github.com/raspberrypi/tools.git中的任何(相当过时的)工具链编译QT5 . 配置脚本因“无法确定体系结构”错误以及包含目录的大量路径问题而失败 . 对我有用的是使用Linaro工具链

    http://releases.linaro.org/components/toolchain/binaries/4.9-2016.02/arm-linux-gnueabihf/runtime-linaro-gcc4.9-2016.02-arm-linux-gnueabihf.tar.xz

    与...结合

    https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py

    无法修复sysroot的符号链接会导致未定义的符号错误,如下所述:An error building Qt libraries for the raspberry pi当我尝试使用tools.git中的fixQualifiedLibraryPaths脚本时,这种情况发生了 . Everthing else在http://wiki.qt.io/RaspberryPi2EGLFS中有详细描述 . 我的配置设置是:

    ./configure -opengl es2 -device linux-rpi3-g -device-option CROSS_COMPILE = / usr / local / rasp / gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf / bin / arm-linux-gnueabihf- - sysroot / usr / local / rasp / sysroot -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -prefix / usr / local / qt5pi -hostprefix / usr / local / qt5pi

    / usr / local / rasp / sysroot是我本地Raspberry Pi 3 Raspbian(Jessie)系统副本的路径,而/ usr / local / qt5pi是交叉编译QT的路径,也必须复制到设备 . 请注意,当您选择工具链时,Jessie会附带GCC 4.9.2 .

  • 0

    您也可以使用clang . 它曾经比GCC更快,现在它是一个非常稳定的东西 . 从源头构建clang要容易得多(在构建过程中你真的可以喝杯咖啡) .

    简而言之:

    • 获取clang二进制文件(sudo apt-get install clang)..或下载并构建(read instructions here

    • 挂载您的raspberry rootfs(它可能是通过sshfs或图像挂载的真正rootfs) .

    • 编译代码:

    path/to/clang --target=arm-linux-gnueabihf --sysroot=/some/path/arm-linux-gnueabihf/sysroot my-happy-program.c -fuse-ld=lld
    

    您可以选择使用旧版arm-linux-gnueabihf binutils . 然后你可以删除末尾的“-fuse-ld = lld”标志 .

    下面是我的cmake工具链文件 .

    toolchain.cmake

    set(CMAKE_SYSTEM_VERSION 1)
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR arm)
    
    # Custom toolchain-specific definitions for your project
    set(PLATFORM_ARM "1")
    set(PLATFORM_COMPILE_DEFS "COMPILE_GLES")
    
    # There we go!
    # Below, we specify toolchain itself!
    
    set(TARGET_TRIPLE arm-linux-gnueabihf)
    
    # Specify your target rootfs mount point on your compiler host machine
    set(TARGET_ROOTFS /Volumes/rootfs-${TARGET_TRIPLE})
    
    # Specify clang paths
    set(LLVM_DIR /Users/stepan/projects/shared/toolchains/llvm-7.0.darwin-release-x86_64/install)
    set(CLANG ${LLVM_DIR}/bin/clang)
    set(CLANGXX ${LLVM_DIR}/bin/clang++)
    
    # Specify compiler (which is clang)
    set(CMAKE_C_COMPILER   ${CLANG})
    set(CMAKE_CXX_COMPILER ${CLANGXX})
    
    # Specify binutils
    
    set (CMAKE_AR      "${LLVM_DIR}/bin/llvm-ar" CACHE FILEPATH "Archiver")
    set (CMAKE_LINKER  "${LLVM_DIR}/bin/llvm-ld" CACHE FILEPATH "Linker")
    set (CMAKE_NM      "${LLVM_DIR}/bin/llvm-nm" CACHE FILEPATH "NM")
    set (CMAKE_OBJDUMP "${LLVM_DIR}/bin/llvm-objdump" CACHE FILEPATH "Objdump")
    set (CMAKE_RANLIB  "${LLVM_DIR}/bin/llvm-ranlib" CACHE FILEPATH "ranlib")
    
    # You may use legacy binutils though.
    #set(BINUTILS /usr/local/Cellar/arm-linux-gnueabihf-binutils/2.31.1)
    #set (CMAKE_AR      "${BINUTILS}/bin/${TARGET_TRIPLE}-ar" CACHE FILEPATH "Archiver")
    #set (CMAKE_LINKER  "${BINUTILS}/bin/${TARGET_TRIPLE}-ld" CACHE FILEPATH "Linker")
    #set (CMAKE_NM      "${BINUTILS}/bin/${TARGET_TRIPLE}-nm" CACHE FILEPATH "NM")
    #set (CMAKE_OBJDUMP "${BINUTILS}/bin/${TARGET_TRIPLE}-objdump" CACHE FILEPATH "Objdump")
    #set (CMAKE_RANLIB  "${BINUTILS}/bin/${TARGET_TRIPLE}-ranlib" CACHE FILEPATH "ranlib")
    
    # Specify sysroot (almost same as rootfs)
    set(CMAKE_SYSROOT ${TARGET_ROOTFS})
    set(CMAKE_FIND_ROOT_PATH ${TARGET_ROOTFS})
    
    # Specify lookup methods for cmake
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    
    # Sometimes you also need this:
    # set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
    
    # Specify raspberry triple
    set(CROSS_FLAGS "--target=${TARGET_TRIPLE}")
    
    # Specify other raspberry related flags
    set(RASP_FLAGS "-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS")
    
    # Gather and distribute flags specified at prev steps.
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CROSS_FLAGS} ${RASP_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CROSS_FLAGS} ${RASP_FLAGS}")
    
    # Use clang linker. Why?
    # Well, you may install custom arm-linux-gnueabihf binutils,
    # but then, you also need to recompile clang, with customized triple;
    # otherwise clang will try to use host 'ld' for linking,
    # so... use clang linker.
    set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld)
    
  • 196

    最初的问题已在很久以前发布,与此同时,Debian在多源支持方面取得了巨大进展 .

    Multiarch是交叉编译的一项伟大成就!

    简而言之,需要以下步骤来利用multiarch进行Raspbian Jessie交叉编译:

    • 在您的Ubuntu主机上安装Debian Jessie amd64在chroot或LXC容器中 .

    • 启用外部架构armhf .

    • 从emdebian工具库安装交叉编译器 .

    • 通过编写自定义gcc规范文件来调整交叉编译器(默认情况下会为ARMv7-A生成代码) .

    • 从Raspbian存储库安装armhf库(libstdc等) .

    • 构建源代码 .

    由于这是很多工作,我自动完成了上述设置 . 你可以在这里读到它:

    Cross Compiling for Raspbian

  • 5

    我创建了最新的预编译Raspberry Pi GCC交叉编译器/本地二进制文件集(最简单的方法):

    Raspberry Pi GCC交叉编译器/本地二进制文件(v8.2.0)

    享受无忧的预编译Raspberry pi GCC交叉编译器,节省您宝贵的时间 . 无需编译或错误处理 . 只需提取,链接并享受完整的GCC(Raspberry Pi)功能机 .

  • 1

    我无法让编译器( x64 版本)使用 sysroot ,直到我将 SET(CMAKE_SYSROOT $ENV{HOME}/raspberrypi/rootfs) 添加到 pi.cmake .

  • 2

    我会尝试将此作为教程编写,因此很容易理解 .

    预先要求

    在开始之前,您需要确保安装以下内容:

    apt-get install git rsync cmake ia32-libs
    

    让我们交叉编译一个馅饼!

    首先在主目录中创建一个名为 raspberrypi 的文件夹 .

    转到此文件夹并下拉上面提到的整个工具文件夹:

    git clone git://github.com/raspberrypi/tools.git
    

    如果我没有读错,你想使用3个中的以下内容, gcc-linaro-arm-linux-gnueabihf-raspbian .

    进入您的主目录并添加:

    export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
    

    到名为 ~/.bashrc 的文件的末尾

    现在您可以注销并重新登录(即重新启动终端会话),或者在终端中运行 . ~/.bashrc 以在当前终端会话中选择 PATH 添加 .

    现在,验证您是否可以访问编译器 arm-linux-gnueabihf-gcc -v . 你应该得到这样的东西:

    Using built-in specs.
    COLLECT_GCC=arm-linux-gnueabihf-gcc
    COLLECT_LTO_WRAPPER=/home/tudhalyas/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../libexec/gcc/arm-linux-gnueabihf/4.7.2/lto-wrapper
    Target: arm-linux-gnueabihf
    Configured with: /cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.b
     uild/src/gcc-linaro-4.7-2012.08/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-
     linux-gnu --target=arm-linux-gnueabihf --prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-l
     inux-gnueabihf-raspbian-linux/install --with-sysroot=/cbuild/slaves/oort61/crosstool-ng/builds/
     arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-languages=c,c++,fo
     rtran --disable-multilib --with-arch=armv6 --with-tune=arm1176jz-s --with-fpu=vfp --with-float=
     hard --with-pkgversion='crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08' --with-bugurl=
     https://bugs.launchpad.net/gcc-linaro --enable-__cxa_atexit --enable-libmudflap --enable-libgom
     p --enable-libssp --with-gmp=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-rasp
     bian-linux/.build/arm-linux-gnueabihf/build/static --with-mpfr=/cbuild/slaves/oort61/crosstool-
     ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpc
     =/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-
     gnueabihf/build/static --with-ppl=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf
     -raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-cloog=/cbuild/slaves/oort61/cros
     stool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --wi
     th-libelf=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/a
     rm-linux-gnueabihf/build/static --with-host-libstdcxx='-L/cbuild/slaves/oort61/crosstool-ng/bui
     lds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static/lib -lpwl' --ena
     ble-threads=posix --disable-libstdcxx-pch --enable-linker-build-id --enable-plugin --enable-gol
     d --with-local-prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-li
     nux/install/arm-linux-gnueabihf/libc --enable-c99 --enable-long-long
    Thread model: posix
    gcc version 4.7.2 20120731 (prerelease) (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08
     )
    

    但是嘿!我做到了,libs仍然无法正常工作!

    我们还没有完成!到目前为止,我们只完成了基础知识 .

    raspberrypi 文件夹中,创建一个名为 rootfs 的文件夹 .

    现在,您需要将整个 /lib/usr 目录复制到此新创建的文件夹中 . 我通常带上rpi图像并通过rsync复制它:

    rsync -rl --delete-after --safe-links pi@192.168.1.PI:/{lib,usr} $HOME/raspberrypi/rootfs
    

    其中 192.168.1.PI 被您的Raspberry Pi的IP替换 .

    现在,我们需要编写一个 cmake 配置文件 . 在您喜欢的编辑器中打开 ~/home/raspberrypi/pi.cmake 并插入以下内容:

    SET(CMAKE_SYSTEM_NAME Linux)
    SET(CMAKE_SYSTEM_VERSION 1)
    SET(CMAKE_C_COMPILER $ENV{HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc)
    SET(CMAKE_CXX_COMPILER $ENV{HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++)
    SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/raspberrypi/rootfs)
    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 程序: -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake .

    使用cmake hello world示例:

    git clone https://github.com/jameskbride/cmake-hello-world.git 
    cd cmake-hello-world
    mkdir build
    cd build
    cmake -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake ../
    make
    scp CMakeHelloWorld pi@192.168.1.PI:/home/pi/
    ssh pi@192.168.1.PI ./CMakeHelloWorld
    
  • 0

    有一个CDP Studio IDE可以从windows和linux进行交叉编译和部署非常简单,你可以在安装过程中检查raspberry工具链复选框 . (PS . 它有GPIO和I2C支持,所以不需要代码来访问它们)

    树莓使用的IDE演示在这里:https://youtu.be/4SVZ68sQz5U

    你可以在这里下载IDE:https://cdpstudio.com/home-edition

  • 4

    对于Windows主机,我强烈推荐this tutorial:

    • 下载并安装工具链

    • 将sysroot与RPi include / lib目录同步

    • 编译代码

    • 使用SmarTTY将可执行文件拖放到RPi

    • 跑吧!

    没有更多,没有更少!

    预构建的GNU工具链可用于Raspberry,Beaglebone,Cubieboard,AVR(Atmel)等

相关问题