C++ 树莓派在 GCC 中的交叉编译。从哪儿开始?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10973020/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:41:23  来源:igfitidea点击:

Cross-compilation for Raspberry Pi in GCC. Where to start?

c++gccarmcross-compilingraspberry-pi

提问by

TL/DR: Where can I find more information about building a GCC 4.7.0 cross-compiling toolchain for ARM (gnueabi) platform (intended to run on a Raspberry Pi device)?

TL/DR:在哪里可以找到有关为 ARM (gnueabi) 平台(打算在 Raspberry Pi 设备上运行)构建 GCC 4.7.0 交叉编译工具链的更多信息?

I have just got a brand new Raspberry Pi and I am very eager to start programming for it. I've managed to install the GCC toolchain (I am using the Arch Linux system image) and compiled some basic programs, all working fine.

我刚刚有了一个全新的 Raspberry Pi,我非常渴望开始为它编程。我设法安装了 GCC 工具链(我使用的是 Arch Linux 系统映像)并编译了一些基本程序,一切正常。

I've also tried to compile the Boost libraries because I often use them in my projects and everything seemed to work fine by following the instructions (./bootstrap.sh+ ./b2) except for the fact that the compilation was painfully slow. I left it on for a few hours but it barely got past the first few source files. After I left it running for the night, I discovered that the build process aborted due to RAM shortage.

我还尝试编译 Boost 库,因为我经常在我的项目中使用它们,并且按照说明 ( ./bootstrap.sh+ ./b2)一切似乎都可以正常工作,除了编译速度非常慢。我把它打开了几个小时,但它几乎没有通过前几个源文件。在我让它运行了一夜之后,我发现由于 RAM 不足,构建过程中止了。

So, my guess is that Rasp Pi is simply underpowered for compiling something of such size as Boost. So, cross-compilation comes to my mind. However, even though there is a lot of information about ARM cross compilation available online, I find it confusing. Where does one even start?

所以,我的猜测是,Rasp Pi 在编译像 Boost 这样大小的东西时能力不足。所以,我想到了交叉编译。然而,即使网上有很多关于 ARM 交叉编译的信息,我还是觉得很困惑。从哪里开始?

I have a recent GCC version (4.7.0) available on my Raspberry Pi, so I would ideally like to cross-compile with the same version. Where can I get the GCC 4.7.0 toolchain for ARM? (I will be compiling on x86 CentOS 6.2)

我的 Raspberry Pi 上有一个最新的 GCC 版本 (4.7.0),因此我希望使用相同版本进行交叉编译。在哪里可以获得 ARM 的 GCC 4.7.0 工具链?(我将在 x86 CentOS 6.2 上编译)

Edit:

编辑:

I deallocated unneeded GPU memory and set up a 4GB swap partition on a USB drive, while build files are on a NFS share. Boost is now compiling much much faster, so it is manageable. I would still like to know how can I set up a GCC 4.7 toolchain for cross compilation on my x86 PC though, since I intend to do a lot of compiling and I would like it to be as fast as possible.

我释放了不需要的 GPU 内存并在 USB 驱动器上设置了一个 4GB 交换分区,而构建文件位于 NFS 共享上。Boost 现在的编译速度要快得多,因此它是易于管理的。我仍然想知道如何在我的 x86 PC 上设置 GCC 4.7 工具链以进行交叉编译,因为我打算进行大量编译并且我希望它尽可能快。

Edit 2:

编辑2:

Since GCC 4.7.0 is relatively new, there does not seem to be a pre-built cross-compiler (i386->ARM). I will probably have to build one myself, which seems an non-trivial task (I've tried and failed). Does anyone know of a tutorial to follow for building a GCC cross-compiler, hopefully for one of the recent versions?

由于 GCC 4.7.0 相对较新,似乎没有预构建的交叉编译器(i386->ARM)。我可能不得不自己构建一个,这似乎是一项不平凡的任务(我已经尝试过但失败了)。有谁知道构建 GCC 交叉编译器的教程,希望是最新版本之一?

I've tried with this great shell script(which worked great for building a same-arch compiler) and I've successfully built binutilsand GCC's prerequisites, but then GCC build kept failing with many cryptic errors. I am really lost here, so I would greatly appreciate your help.

我已经尝试过这个很棒的 shell 脚本(它非常适合构建相同架构的编译器)并且我已经成功构建了binutilsGCC 的先决条件,但是随后 GCC 构建一直失败并出现许多神秘的错误。我真的迷路了,所以我非常感谢你的帮助。

GCC on Raspberry Pi was configured with

Raspberry Pi 上的 GCC 配置为

--prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib 
--mandir=/usr/share/man --infodir=/usr/share/info 
--with-bugurl=https://bugs.archlinux.org/ 
--enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-shared 
--enable-threads=posix --with-system-zlib --enable-__cxa_atexit 
--disable-libunwind-exceptions --enable-clocale=gnu 
--disable-libstdcxx-pch --enable-libstdcxx-time 
--enable-gnu-unique-object --enable-linker-build-id --with-ppl 
--enable-cloog-backend=isl --enable-lto --enable-gold 
--enable-ld=default --enable-plugin --with-plugin-ld=ld.gold 
--with-linker-hash-style=gnu --disable-multilib --disable-libssp 
--disable-build-with-cxx --disable-build-poststage1-with-cxx 
--enable-checking=release --host=arm-unknown-linux-gnueabi 
--build=arm-unknown-linux-gnueabi 

Edit 3:

编辑3:

I managed to build a 4.7 GCC toolchain for ARM (yay!) using this shell scriptas suggested by user dwelch in the comments. I also built newliband libstdc++using this articleas a guide. The toolchain works fine, but hen I run the executable on my Raspberry Pi, it fails with Illegal instruction. What could be the cause of that?

我设法按照用户 dwelch 在评论中的建议,使用这个 shell 脚本为 ARM 构建了一个 4.7 GCC 工具链(是的!)。我还构建newliblibstdc++使用本文作为指南。工具链工作正常,但是当我在 Raspberry Pi 上运行可执行文件时,它失败并显示Illegal instruction. 可能是什么原因造成的?

回答by

I found these instructions How to build a cross compiler for your Raspberry Pi. It is a great walk through using a crosstool-ngtool which simplifies configuring a cross-compiler build A LOT (it has a nice curses-based interface) and it supports GCC 4.7.

我找到了这些说明如何为您的 Raspberry Pi 构建交叉编译器。这是一个很好的使用crosstool-ng工具,它简化了交叉编译器构建的配置(它有一个很好的基于curses的界面)并且它支持GCC 4.7。

Seems to work great!

看起来效果很好!

回答by old_timer

http://github.com/dwelch67/raspberrypibuildgcc directorythere is a script for both 4.7 gcc/gnu and 3.0 clang/llvm. gnu one derived from the script at the mpx project at opencores, I cut out gdb and libgcc, glibc, etc took it down to a compiler. if you want that other stuff cut and paste arm for mips.

http://github.com/dwelch67/raspberrypi buildgcc 目录中有 4.7 gcc/gnu 和 3.0 clang/llvm 的脚本。gnu 是从 opencores 的 mpx 项目中的脚本派生出来的,我删掉了 gdb 和 libgcc、glibc 等,将其归结为编译器。如果你想要其他东西剪切和粘贴用于 mips 的手臂。

回答by Mikhail Kupchik

Here is a step-by-step guide How to build Raspberry Pi cross-compiler in Windows. The reason you are getting Illegal Instructionerror is latest Raspbian is hardfp-enabled and requires appropriate patches for gcc and eglibc to support hardfp. Otherwise generated code will use different ABI, i.e. pass function arguments in different registers, so crash at runtime.

这是如何在 Windows 中构建 Raspberry Pi 交叉编译器的分步指南。您收到Illegal Instruction错误的原因是最新的 Raspbian 启用了 hardfp,并且需要适当的 gcc 和 eglibc 补丁来支持 hardfp。否则生成的代码将使用不同的 ABI,即在不同的寄存器中传递函数参数,因此在运行时崩溃。

Also misconfiguring GCC for ARMv7 (Raspebby Pi is ARMv6) may lead to Illegal Instructionerror. Be sure to specify --with-arch=armv6option when configuring GCC.

此外,为 ARMv7 错误配置 GCC(Raspebby Pi 是 ARMv6)可能会导致Illegal Instruction错误。--with-arch=armv6配置 GCC 时一定要指定选项。

The guide linked above is based on GCC 4.6.3 though. But I guess it should work with GCC 4.7 too.

不过,上面链接的指南基于 GCC 4.6.3。但我想它也应该适用于 GCC 4.7。

回答by abhiTronix

You can try my open-sourced pre-built/pre-compiled GCC Cross & Native Compiler Binaries for Raspberry Pi from this up-to-date GitHub repository:

您可以从这个最新的GitHub 存储库尝试我的开源预构建/预编译 GCC Cross & Native Compiler Binaries for Raspberry Pi :

This project contains the UpToDate set of Precompiled/Pre-Built Raspberry pi GCC Cross & Native Compilers Binaries, saving your tons of time(No compiling or Error Handling needed whatsoever). Just Extract, Link & Enjoy complete GCC(Raspberry Pi) functionality in your Machine. You can use its native compilers for Raspberry Pi(Can be used along with old & slow 6.3.0 GCC), Or use the Cross-Compiler in any Linux Machine(Tested on Latest Ubuntu/bionic x64) to compile programs for your Raspberry Pi. All these compilers binaries are Raspberry Pi hardware optimized for enhanced overall performance.

该项目包含 UpToDate 预编译/预构建 Raspberry pi GCC Cross & Native Compilers Binaries 集,节省您的大量时间(无需编译或错误处理)。只需在您的机器中提取、链接和享受完整的 GCC(Raspberry Pi)功能。您可以将其本地编译器用于 Raspberry Pi(可以与旧的和慢速 6.3.0 GCC 一起使用),或者在任何 Linux 机器上使用交叉编译器(在最新的 Ubuntu/bionic x64 上测试)为您的 Raspberry Pi 编译程序. 所有这些编译器二进制文件都是为增强整体性能而优化的 Raspberry Pi 硬件。

Supported GCC Versions:

支持的 GCC 版本:

  • GCC 6.3.0
  • GCC 7.4.0
  • GCC 8.2.0
  • GCC 8.3.0
  • 海湾合作委员会 6.3.0
  • 海湾合作委员会 7.4.0
  • 海湾合作委员会 8.2.0
  • 海湾合作委员会 8.3.0

Supported Raspberry Pis:

支持的树莓派:

  • All Raspberry Pi versions/models are currently supported.
  • Any other ARM Devices with similar Hardware configurations may also work.
  • 当前支持所有 Raspberry Pi 版本/型号。
  • 具有类似硬件配置的任何其他 ARM 设备也可以工作。

Supported Environments:

支持的环境:

  • Cross-Compiler:All Linux Distros (x32/x64) are currently supported.
  • Native-Compiler:All Raspberry Pi version/model with Raspbian OS is supported. Other OS may/may-not work.
  • 交叉编译器:当前支持所有 Linux 发行版 (x32/x64)。
  • 本机编译器:支持所有带有 Raspbian 操作系统的 Raspberry Pi 版本/型号。其他操作系统可能/可能不工作。

Supported Languages:

支持的语言:

  • C++
  • C
  • Fortran
  • C++
  • C
  • 复式

回答by NickT

I couldn't find a 4.7 gcc, only a 4.6. For information, I compiled the Pi's kernel on a fairly old Ubuntu machine (1Ghz, 768MB RAM) following the guidelines on this page: Kernel compilation

我找不到 4.7 gcc,只有 4.6。有关信息,我按照此页面上的指南在相当旧的 Ubuntu 机器(1Ghz,768MB RAM)上编译了 Pi 的内核: 内核编译

The build took about 90 minutes, compared to the 6 hours it is suggested that it would take on the Pi. The kernel runs fine on the Pi. Perhaps you can adapt these guidelines for cross-compiling other projects on your x86 Linux machine.

构建耗时约 90 分钟,相比之下,建议在 Pi 上耗时 6 小时。内核在 Pi 上运行良好。也许您可以调整这些指南,以便在 x86 Linux 机器上交叉编译其他项目。

回答by kallaballa

You could try the Raspberry-GCC-4.7.3toolchain.

您可以尝试使用Raspberry-GCC-4.7.3工具链。

It's a pre-built gcc-4.7.3 toolchain for armv6 with hardfp using gnueabi. I'm using it to cross compile c++11 for a raspberrian target.

它是一个预构建的 gcc-4.7.3 工具链,用于使用 gnueabi 的带有 hardfp 的 armv6。我正在使用它为 raspberrian 目标交叉编译 c++11。

Please note it only works on linux x86_64 hosts.

请注意它仅适用于 linux x86_64 主机。

回答by Zeev Glozman

The dockcross projectis a great starting point for a completely deodorized tool chain, you could build not just for Pi but for anything else. Here are the commands:

dockcross项目是一个完全除臭工具链一个很好的起点,你可以建立不只是为皮,但为别的。以下是命令:

docker run --rm dockcross/linux-armv7 > ./dockcross-linux-armv7
chmod +x ./dockcross-linux-armv7
./dockcross-linux-armv7 bash -c '$CC test/C/hello.c -o hello_arm'

It has cmakebuilt in as well.

它也cmake内置了。

回答by mlepage

As you've noted, building on the Raspberry Pi itself is slow, but reliable (since it's not cross-compiling), provided you don't run out of memory. Perhaps it's possible to tweak the memory and swap settings to make larger builds possible (if not fast). I know on the forums, people are talking about putting root partitions on SD, user partitions on USB drives, and of course it will probably make a difference to get a fast (class 6 or better) SD card. It's possible, over time, that the compilation performance will improve as the software improves. Or, maybe Raspberry Pi 2 will have an improved chipset and more RAM. :-)

正如您所指出的,在 Raspberry Pi 本身上构建很慢,但很可靠(因为它不是交叉编译),前提是您不会耗尽内存。也许可以调整内存和交换设置以实现更大的构建(如果不是很快)。我知道在论坛上,人们谈论将根分区放在 SD 上,将用户分区放在 USB 驱动器上,当然,获得快速(6 级或更好)的 SD 卡可能会有所不同。随着时间的推移,编译性能可能会随着软件的改进而提高。或者,也许 Raspberry Pi 2 将拥有改进的芯片组和更多的 RAM。:-)