如何在我的 Linux 主机上安装树莓派交叉编译器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19162072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install the Raspberry Pi cross compiler on my Linux host machine?
提问by pqvst
I am attempting to get cross-compiling for Raspberry Pi working on my Ubuntu machine.
我正在尝试在我的 Ubuntu 机器上对 Raspberry Pi 进行交叉编译。
During my initial attempts I was using the arm-linux-gnueabi compiler, which is available in the Ubuntu repo. I got this working. I was able to build all my dependencies and use the cross-compiler in my cmake project.
在我最初的尝试中,我使用了 Ubuntu 存储库中提供的 arm-linux-gnueabi 编译器。我得到了这个工作。我能够构建所有依赖项并在我的 cmake 项目中使用交叉编译器。
However, I believe I should be using the hf version, so I switched to arm-linux-gnueabihf. Then I realized that this does not work with Raspberry Pi since it is armv6.
但是,我相信我应该使用 hf 版本,所以我切换到 arm-linux-gnueabihf。然后我意识到这不适用于 Raspberry Pi,因为它是 armv6。
After some Googling, I then found the pre-built toolchain from GitHub.
I downloaded the toolchain, but I don't really understand how to "install" it. I extracted the files to my home directory. The directory structure looks like this:
我下载了工具链,但我真的不明白如何“安装”它。我将文件解压缩到我的主目录。目录结构如下所示:
/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)
If I change directory to the INNER bin folder I am able to compile a test program from the terminal without any problems.
如果我将目录更改为 INNER bin 文件夹,则可以从终端编译测试程序而不会出现任何问题。
~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/
arm-linux-gnueabihf/bin$ g++ test.cpp -o test
I then tried to compile a test program in the OUTER bin folder, which contains the prefixed versions of the tools.
然后我尝试在 OUTER bin 文件夹中编译一个测试程序,其中包含工具的前缀版本。
~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin$
arm-linux-gnueabihf-g++ test.cpp -o test
However, when I try to use the compiler now (from outside the inner bin directory), it is unable to find the libstdc++ shared library that comes with the toolchain:
但是,当我现在尝试使用编译器时(从内部 bin 目录之外),却找不到工具链附带的 libstdc++ 共享库:
arm-linux-gnueabihf-gcc: error while loading shared libraries:
libstdc++.so.6: cannot open shared object file: No such file or directory.
Furthermore, I want to be able to use the compiler without having to navigate to the bin directory. So I tried adding the OUTER bin directory (since I want the prefixed versions) and both lib directories to my PATH:
此外,我希望能够使用编译器而不必导航到 bin 目录。所以我尝试将 OUTER bin 目录(因为我想要带前缀的版本)和两个 lib 目录添加到我的 PATH:
export PATH=$PATH:~/tools/.../bin
export PATH=$PATH:~/tools/.../lib
export PATH=$PATH:~/tools/.../.../lib
However, this results in the same error. How should I "install" the toolchain so that I can use the toolchain from everywhere, just like I can when I use the cross-compilers from the Ubuntu repo?
但是,这会导致相同的错误。我应该如何“安装”工具链,以便我可以从任何地方使用工具链,就像我使用 Ubuntu 存储库中的交叉编译器一样?
采纳答案by Stenyg
I'm gonna try to write this as a tutorial for you so it becomes easy to follow.
我会试着把它写成一个教程给你,这样它就变得容易理解了。
NOTE: This tutorial only works for older raspbian images. For the newer Raspbian based on Debian Buster see the following how-to in this thread: https://stackoverflow.com/a/58559140/869402
注意:本教程仅适用于较旧的 raspbian 映像。对于基于 Debian Buster 的较新 Raspbian,请参阅此线程中的以下操作方法:https: //stackoverflow.com/a/58559140/869402
Pre-requirements
先决条件
Before you start you need to make sure the following is installed:
在开始之前,您需要确保已安装以下内容:
apt-get install git rsync cmake ia32-libs
Let's cross compile a Pie!
让我们交叉编译一个 Pie!
Start with making a folder in your home directory called raspberrypi
.
首先在您的主目录中创建一个名为raspberrypi
.
Go in to this folder and pull down the ENTIRE tools folder you mentioned above:
进入这个文件夹并下拉你上面提到的整个工具文件夹:
git clone git://github.com/raspberrypi/tools.git
You wanted to use the following of the 3 ones, gcc-linaro-arm-linux-gnueabihf-raspbian
, if I did not read wrong.
gcc-linaro-arm-linux-gnueabihf-raspbian
如果我没有读错,您想使用以下 3 个,。
Go into your home directory and add:
进入您的主目录并添加:
export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
to the end of the file named ~/.bashrc
到名为的文件的末尾 ~/.bashrc
Now you can either log out and log back in (i.e. restart your terminal session), or run . ~/.bashrc
in your terminal to pick up the PATH
addition in your current terminal session.
现在,您可以注销并重新登录(即重新启动终端会话),或者. ~/.bashrc
在终端中运行以PATH
在当前终端会话中选择添加项。
Now, verify that you can access the compiler arm-linux-gnueabihf-gcc -v
. You should get something like this:
现在,验证您是否可以访问编译器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
)
But hey! I did that and the libs still don't work!
但是嘿!我这样做了,但库仍然无法正常工作!
We're not done yet! So far, we've only done the basics.
我们还没有完成!到目前为止,我们只完成了基础工作。
In your raspberrypi
folder, make a folder called rootfs
.
在您的raspberrypi
文件夹中,创建一个名为rootfs
.
Now you need to copy the entire /lib
and /usr
directory to this newly created folder. I usually bring the rpi image up and copy it via rsync:
现在,您需要将整个复制/lib
和/usr
目录到这个新创建的文件夹。我通常将 rpi 图像调出并通过 rsync 复制它:
rsync -rl --delete-after --safe-links [email protected]:/{lib,usr} $HOME/raspberrypi/rootfs
where 192.168.1.PI
is replaced by the IP of your Raspberry Pi.
where192.168.1.PI
被 Raspberry Pi 的 IP 替换。
Now, we need to write a cmake
config file. Open ~/home/raspberrypi/pi.cmake
in your favorite editor and insert the following:
现在,我们需要编写一个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)
Now you should be able to compile your cmake
programs simply by adding this extra flag: -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake
.
现在,你应该能够编译cmake
程序只需加入这个额外的标志:-D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake
。
Using a cmake hello worldexample:
使用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 [email protected]:/home/pi/
ssh [email protected] ./CMakeHelloWorld
回答by Andreas
I couldn't get the compiler (x64
version) to use the sysroot
until I added SET(CMAKE_SYSROOT $ENV{HOME}/raspberrypi/rootfs)
to pi.cmake
.
我不能让编译器(x64
版本)来使用sysroot
,直到我说SET(CMAKE_SYSROOT $ENV{HOME}/raspberrypi/rootfs)
要pi.cmake
。
回答by cuffel
I could not compile QT5 with any of the (fairly outdated) toolchains from git://github.com/raspberrypi/tools.git. The configure script kept failing with an "could not determine architecture" error and with massive path problems for include directories. What worked for me was using the Linaro toolchain
我无法使用来自 git://github.com/raspberrypi/tools.git 的任何(相当过时的)工具链编译 QT5。配置脚本因“无法确定体系结构”错误和包含目录的大量路径问题而不断失败。对我有用的是使用 Linaro 工具链
in combination with
结合
https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py
https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py
Failing to fix the symlinks of the sysroot leads to undefined symbol errors as described here: An error building Qt libraries for the raspberry piThis happened to me when I tried the fixQualifiedLibraryPaths script from tools.git. Everthing else is described in detail in http://wiki.qt.io/RaspberryPi2EGLFS. My configure settings were:
未能修复 sysroot 的符号链接会导致未定义的符号错误,如下所述:An error building Qt libraries forthe raspberry pi当我从 tools.git 尝试 fixQualifiedLibraryPaths 脚本时发生了这种情况。在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
./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
with /usr/local/rasp/sysroot being the path of my local Raspberry Pi 3 Raspbian (Jessie) system copy and /usr/local/qt5pi being the path of the cross compiled QT that also has to be copied to the device. Be aware that Jessie comes with GCC 4.9.2 when you choose your toolchain.
/usr/local/rasp/sysroot 是我本地 Raspberry Pi 3 Raspbian (Jessie) 系统副本的路径,/usr/local/qt5pi 是交叉编译的 QT 的路径,也必须复制到设备。请注意,当您选择工具链时,Jessie 随附 GCC 4.9.2。
回答by SBF
For Windows host, I want to highly recommend this tutorial::
对于 Windows 主机,我要强烈推荐这个教程::
- Download and install the toolchain
- Sync sysroot with your RPi include/lib directories
- Compile your code
- Drag and drop the executable to your RPi using SmarTTY
- Run it!
Nothing more, nothing less!
不多也不少!
Prebuilt GNU Toolchains available for Raspberry, Beaglebone, Cubieboard, AVR (Atmel) and more
可用于 Raspberry、Beaglebone、Cubieboard、AVR (Atmel) 等的预构建 GNU 工具链
回答by Matthias Lüscher
The initial question has been posted quite some time ago and in the meantime Debian has made huge headway in the area of multiarch support.
最初的问题已经在很久以前发布了,与此同时 Debian 在多架构支持领域取得了巨大进展。
Multiarch is a great achievement for cross compilation!
Multiarch 是交叉编译的一大成就!
In a nutshell the following steps are required to leverage multiarch for Raspbian Jessie cross compilation:
简而言之,需要以下步骤来利用 multiarch 进行 Raspbian Jessie 交叉编译:
- On your Ubuntu host install Debian Jessie amd64 within a chroot or a LXC container.
- Enable the foreign architecture armhf.
- Install the cross compiler from the emdebian tools repository.
- Tweak the cross compiler (it would generate code for ARMv7-A by default) by writing a custom gcc specs file.
- Install armhf libraries (libstdc++ etc.) from the Raspbian repository.
- Build your source code.
- 在您的 Ubuntu 主机上,在 chroot 或 LXC 容器中安装 Debian Jessie amd64。
- 启用国外架构 armhf。
- 从 emdebian 工具库安装交叉编译器。
- 通过编写自定义 gcc 规范文件来调整交叉编译器(默认情况下它会为 ARMv7-A 生成代码)。
- 从 Raspbian 存储库安装 armhf 库(libstdc++ 等)。
- 构建您的源代码。
Since this is a lot of work I have automated the above setup. You can read about it here:
由于这是很多工作,我已经自动化了上述设置。你可以在这里读到它:
回答by Jüri T
there is a CDP Studio IDE available that makes cross compile and deploy quite simple from both windows and linux and you can just check the raspberry toolchain checkbox during the installation. (PS. it has GPIO and I2C support so no code is needed to access those)
有一个 CDP Studio IDE 可用,它使从 windows 和 linux 的交叉编译和部署变得非常简单,您可以在安装过程中选中 raspberry 工具链复选框。(PS。它具有 GPIO 和 I2C 支持,因此无需代码即可访问它们)
The IDE demo of raspberry use is up here: https://youtu.be/4SVZ68sQz5U
树莓派使用的 IDE 演示在这里:https: //youtu.be/4SVZ68sQz5U
and you can download the IDE here: https://cdpstudio.com/home-edition
您可以在此处下载 IDE:https: //cdpstudio.com/home-edition
回答by Stepan Dyatkovskiy
You may use clangas well. It used to be faster than GCC, and now it is quite a stable thing. It is much easier to build clang from sources (you can really drink cup of coffee during build process).
您也可以使用clang。以前比GCC还快,现在已经是相当稳定的东西了。从源代码构建clang要容易得多(在构建过程中你真的可以喝杯咖啡)。
In short:
简而言之:
- Get clang binaries (sudo apt-get install clang).. or download and build (read instructions here)
- Mount your raspberry rootfs (it may be the real rootfs mounted via sshfs, or an image).
Compile your code:
path/to/clang --target=arm-linux-gnueabihf --sysroot=/some/path/arm-linux-gnueabihf/sysroot my-happy-program.c -fuse-ld=lld
- 获取 clang 二进制文件 (sudo apt-get install clang).. 或下载并构建(在此处阅读说明)
- 挂载你的树莓派 rootfs(它可能是通过 sshfs 挂载的真正的 rootfs,或者是一个镜像)。
编译你的代码:
path/to/clang --target=arm-linux-gnueabihf --sysroot=/some/path/arm-linux-gnueabihf/sysroot my-happy-program.c -fuse-ld=lld
Optionally you may use legacy arm-linux-gnueabihf binutils. Then you may remove "-fuse-ld=lld" flag at the end.
您可以选择使用旧版 arm-linux-gnueabihf binutils。然后你可以在最后删除“-fuse-ld=lld”标志。
Below is my cmake toolchain file.
下面是我的 cmake 工具链文件。
toolchain.cmake
工具链.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)
回答by Stefan Profanter
Building for newer Raspbian Debian Buster images and ARMv6
为较新的 Raspbian Debian Buster 映像和 ARMv6 构建
The answer by @Stenyg only works for older Raspbian images. The recently released Raspbian based on Debian Buster requires an updated toolchain:
@Stenyg 的答案仅适用于较旧的 Raspbian 图像。最近发布的基于 Debian Buster 的 Raspbian 需要更新的工具链:
In Debian Buster the gcc compiler and glibc was updated to version 8.3. The toolchain in git://github.com/raspberrypi/tools.git
is still based on the older gcc 6 version. This means that using git://github.com/raspberrypi/tools.git
will lead to many compile errors.
在 Debian Buster 中,gcc 编译器和 glibc 已更新到版本 8.3。中的工具链git://github.com/raspberrypi/tools.git
仍然基于较旧的 gcc 6 版本。这意味着使用git://github.com/raspberrypi/tools.git
会导致很多编译错误。
This tutorial is based on @Stenyg answer. In addition to many other solutions in the internet, this tutorial also supports older Rasperry Pi (A, B, B+, Zero) based on the ARMv6 CPU. See also: GCC 8 Cross Compiler outputs ARMv7 executable instead of ARMv6
本教程基于@Stenyg 答案。除了互联网上的许多其他解决方案,本教程还支持基于 ARMv6 CPU 的较旧的 Rasperry Pi(A、B、B+、零)。另请参阅: GCC 8 交叉编译器输出 ARMv7 可执行文件而不是 ARMv6
Set up the toolchain
设置工具链
There is no official git repository containing an updated toolchain (See https://github.com/raspberrypi/tools/issues/102).
没有包含更新工具链的官方 git 存储库(请参阅https://github.com/raspberrypi/tools/issues/102)。
I created a new github repository which includes building and precompiled toolchains for ARMv6 based on GCC8 and newer:
我创建了一个新的 github 存储库,其中包括基于 GCC8 和更新版本的 ARMv6 构建和预编译工具链:
https://github.com/Pro/raspi-toolchain
https://github.com/Pro/raspi-toolchain
As mentioned in the project's readme, these are the steps to get the toolchain. You can also build it yourself (see the README for further details).
正如项目的自述文件中提到的,这些是获取工具链的步骤。您也可以自己构建它(有关更多详细信息,请参阅自述文件)。
- Download the toolchain:
- 下载工具链:
wget https://github.com/Pro/raspi-toolchain/releases/latest/download/raspi-toolchain.tar.gz
- Extract it. Note: The toolchain has to be in
/opt/cross-pi-gcc
since it's not location independent.
- 提取它。注意:工具链必须在,
/opt/cross-pi-gcc
因为它不是位置无关的。
sudo tar xfz raspi-toolchain.tar.gz --strip-components=1 -C /opt
You are done! The toolchain is now in
/opt/cross-pi-gcc
Optional, add the toolchain to your path, by adding:
你完成了!工具链现在在
/opt/cross-pi-gcc
可选地,通过添加以下内容将工具链添加到您的路径中:
export PATH=$PATH:/opt/cross-pi-gcc/bin
to the end of the file named ~/.bashrc
到名为的文件的末尾 ~/.bashrc
Now you can either log out and log back in (i.e. restart your terminal session), or run . ~/.bashrc
in your terminal to pick up the PATH
addition in your current terminal session.
现在,您可以注销并重新登录(即重新启动终端会话),或者. ~/.bashrc
在终端中运行以PATH
在当前终端会话中选择添加项。
Get the libraries from the Raspberry PI
从 Raspberry PI 获取库
To cross-compile for your own Raspberry Pi, which may have some custom libraries installed, you need to get these libraries onto your host.
要为您自己的 Raspberry Pi(可能安装了一些自定义库)进行交叉编译,您需要将这些库放到您的主机上。
Create a folder $HOME/raspberrypi
.
In your raspberrypi
folder, make a folder called rootfs
.
创建一个文件夹$HOME/raspberrypi
。在您的raspberrypi
文件夹中,创建一个名为rootfs
.
Now you need to copy the entire /lib
and /usr
directory to this newly created folder. I usually bring the rpi image up and copy it via rsync:
现在,您需要将整个复制/lib
和/usr
目录到这个新创建的文件夹。我通常将 rpi 图像调出并通过 rsync 复制它:
rsync -vR --progress -rl --delete-after --safe-links [email protected]:/{lib,usr,opt/vc/lib} $HOME/raspberrypi/rootfs
where 192.168.1.PI
is replaced by the IP of your Raspberry Pi.
where192.168.1.PI
被 Raspberry Pi 的 IP 替换。
Use CMake to compile your project
使用 CMake 编译您的项目
To tell CMake to take your own toolchain, you need to have a toolchain file which initializes the compiler settings.
要告诉 CMake 使用您自己的工具链,您需要有一个工具链文件来初始化编译器设置。
Get this toolchain file from here: https://github.com/Pro/raspi-toolchain/blob/master/Toolchain-rpi.cmake
从这里获取这个工具链文件:https: //github.com/Pro/raspi-toolchain/blob/master/Toolchain-rpi.cmake
Now you should be able to compile your cmake
programs simply by adding this extra flag: -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake
and setting the correct environment variables:
现在你应该能够cmake
通过添加这个额外的标志来编译你的程序:-D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake
并设置正确的环境变量:
export RASPBIAN_ROOTFS=$HOME/raspberry/rootfs
export PATH=/opt/cross-pi-gcc/bin:$PATH
export RASPBERRY_VERSION=1
cmake -DCMAKE_TOOLCHAIN_FILE=$HOME/raspberry/Toolchain-rpi.cmake ..
An example hello world is shown here: https://github.com/Pro/raspi-toolchain/blob/master/build_hello_world.sh
这里显示了一个示例 hello world:https: //github.com/Pro/raspi-toolchain/blob/master/build_hello_world.sh