Raspi 的交叉编译 - 执行程序以“分段错误”结束

ala*_*ack 5 c++ linux compilation cross-compiling raspberry-pi

我有一个自己编写的程序,我也想从我的 x86 机器上为 Raspberry Pi 构建它。我正在使用 eclipse 生成的 makefile 并且无法更改此内容。

我已经阅读了 raspi 的 CC 教程:Hackaday-Link。因为raspi也安装了4.9版的gcc,我也用这个版本的交叉编译器试了一下。这个 hello world 程序也存在这个问题:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    cout << "hello world!" << endl;
}
Run Code Online (Sandbox Code Playgroud)

直接在 raspi 上编译和运行时,输出为hello world!. 没关系。但是当用 4.9 的版本交叉编译它arm-linux-gnueabihf-g++-4.9,然后将它 scp 到 raspi,使其可执行并运行它,输出./hello_worldSegmentation fault. 执行sudo ./hello_world时没有输出。

我试图获取有关文件的一些信息,并看到 raspi 编译程序的本地输出:

pi@raspberrypi:~ $ file hello_world
hello_world: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[sha1]=41161ae762d940b12c3313ca065a3badd284f6d3, not stripped
Run Code Online (Sandbox Code Playgroud)

和交叉编译的版本输出

pi@raspberrypi:~ $ file hello_world
hello_world: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=4f1f4fb86710ef8130f148dc5adae1c0c18092fd, not stripped
Run Code Online (Sandbox Code Playgroud)

谁能告诉我问题是什么以及如何解决它?

nor*_*hov 2

工具链编译器arm-linux-gnueabihf-gcc可以有不同的默认参数来运行:

 arm-linux-gnueabihf-gcc -Q --help=target
Run Code Online (Sandbox Code Playgroud)

编译器安装在 Raspberry Stretch 上(我只留下基本信息):

-march= armv6 -marm [enabled] -mfloat-abi= hard -mfp16-format= none -mfpu= vfp

拉伸默认交叉编译器:

-march= armv7-a -marm [disabled] -mfloat-abi= hard -mfp16-format= none -mfpu= vfpv3-d16

现在您可以看到架构上的差异了。因此,要使用交叉编译器进行 Sompile,需要进行设置march以匹配您所需的 CPU。另请注意,Debian 交叉编译器默认生成 Thumb 代码,Raspberry Stretch 默认生成 ARM 代码

我建议您在设备不支持时针对较新的 CPU 系列进行交叉编译。