C++ 树莓派的交叉编译

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

Cross-Compiling for RaspBerry Pi

c++cross-compilingcodesourceryraspberry-pi

提问by Cesar Ortiz

With a RaspBerry Pi and from my computer, I'm trying to cross-compile a simple helloWorld written in C++. I'm using Code Sourcery toolchain for linux to compile.

使用 RaspBerry Pi 和我的电脑,我试图交叉编译一个用 C++ 编写的简单 helloWorld。我正在使用适用于 linux 的 Code Sourcery 工具链进行编译。

When copy the helloWorld binary to raspBerry by TFTP and give it execution permissions with chmod, the next error appears:

当通过 TFTP 将 helloWorld 二进制文件复制到 raspBerry 并使用 chmod 为其授予执行权限时,出现下一个错误:

"Illegal instruction"

“非法指令”

If make a 'file' over binary I get: "raspberry: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, stripped"

如果在二进制文件上创建一个“文件”,我会得到:“树莓:ELF 32 位 LSB 可执行文件,ARM,版本 1(SYSV),静态链接,剥离”

This is because I used "-static -static-libstdc++" when linking.

这是因为我在链接时使用了“-static -static-libstdc++”。

If I don't use static linking, the error is: "Segmentation fault"

如果我不使用静态链接,错误是:“分段错误”

The Code:

编码:

/*
 * main.cpp
 *
 *  Created on: 26/06/2012
 *      Author: ccortiz
 */

#include <iostream>
using namespace std;

int main(void){

    cout << "Hello Cross Compilling for ARM!" << endl << flush;
    return 0;
}

How could I compile and run my program in a right way? Thanks.

我怎样才能以正确的方式编译和运行我的程序?谢谢。

回答by Mikhail Kupchik

The reason why are you getting Segmentation faulterror is different ABI. Raspberry Pi when running Raspbian is using linux-arm-gnueabihfABI which assumes hardfp and VFP support in hardware (which is rare in ARMv6 environment) so requires some additional patches for GCC and EGLIBC (these patches can be found in Raspbian repository).

您收到Segmentation fault错误的原因是不同的 ABI。运行 Raspbian 时的 Raspberry Pi 使用linux-arm-gnueabihfABI,它假定硬件支持 hardfp 和 VFP(这在 ARMv6 环境中很少见),因此需要一些额外的 GCC 和 EGLIBC 补丁(这些补丁可以在 Raspbian 存储库中找到)。

Your Code Sourcery cross-toolchain most likely does not have these patches, so it's using another ABI (linux-arm-gnueabi) hence the crash at runtime (static linking works because kernel ABI does not depend on hardfp/softfp).

您的 Code Sourcery 跨工具链很可能没有这些补丁,因此它使用另一个 ABI ( linux-arm-gnueabi) 因此在运行时崩溃(静态链接有效,因为内核 ABI 不依赖于 hardfp/softfp)。

Another possible reason why you may be getting Illegal Instructionerror is Code Sourcery cross-toolchain configured for ARMv7 and Raspberry Pi is ARMv6. But in this case both static and dynamic linking will yield the same error.

出现Illegal Instruction错误的另一个可能原因是为 ARMv7 和 Raspberry Pi 配置的 Code Sourcery 跨工具链是 ARMv6。但在这种情况下,静态和动态链接都会产生相同的错误。

Here is a step-by-step guide how to build Raspberry Pi cross compiler in Windows, both hardfp/softfp ABI versions. Resulting cross-compiler supports C++ and does not depend on cygwin runtime library (cygwin1.dll).

这是如何在 Windows 中构建 Raspberry Pi 交叉编译器的分步指南,包括 hardfp/softfp ABI 版本。生成的交叉编译器支持 C++ 并且不依赖于 cygwin 运行时库 (cygwin1.dll)。

回答by hithwen

I'd recommend trying biicode, it automatically sets up the cross compiler environment for you and sends generated binaries to the raspberry after building

我建议尝试biicode,它会自动为您设置交叉编译器环境,并在构建后将生成的二进制文件发送到raspberry

回答by Cesar Ortiz

The problem was to use ASCII mode instead of binary mode in my FTP transfers. Today I prefer to use SFTP (SSH). Thanks.

问题是在我的 FTP 传输中使用 ASCII 模式而不是二进制模式。今天我更喜欢使用 SFTP (SSH)。谢谢。

回答by kallaballa

You could try the toolchain at: https://github.com/kallaballa/Raspberry-GCC-4.7.3

您可以在以下位置尝试工具链:https: //github.com/kallaballa/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 Hatto

My recommendation is here, http://hertaville.com/2012/09/28/development-environment-raspberry-pi-cross-compiler/He does a good job on explaining how to use Eclipse to compile HelloWorld.cpp, download, and execute it in Raspberry pi board. Check it out.

我的推荐在这里,http://hertaville.com/2012/09/28/development-environment-raspberry-pi-cross-compiler/他很好地解释了如何使用 Eclipse 编译 HelloWorld.cpp,下载,并在树莓派板上执行。一探究竟。

回答by Arun Chand

Using Visual studio and Visual GDB plugin you can download cross compiler for raspberry pi. After that you can create a ssh connection and also you can deploy your program remotely into raspberry pi.

使用 Visual Studio 和 Visual GDB 插件,您可以下载树莓派的交叉编译器。之后,您可以创建一个 ssh 连接,也可以将您的程序远程部署到 raspberry pi 中。