Raspi 的交叉编译 - 执行程序以 "Segmentation fault" 结束

Cross Compiling for Raspi - Executing the programm ends in "Segmentation fault"

本文关键字:Segmentation fault 结束 执行程序 交叉编译 Raspi      更新时间:2023-10-16

我有一个自己编写的程序,我想在我的x86机器上为树莓派构建它。我使用的是eclipse生成的makefiles,不能更改这个东西。

我已经阅读了这个CC for raspi的教程:hackday - link。因为raspi也安装了gcc 4.9版本,所以我也尝试使用这个版本的交叉编译器。这个hello world程序也存在这个问题:

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    cout << "hello world!" << endl;
}

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

我试着得到一些关于文件的信息,看到在本地的树莓编译程序输出:

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

和交叉编译的版本输出

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
谁能告诉我问题是什么以及如何解决它?

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

 arm-linux-gnueabihf-gcc -Q --help=target

编译器安装在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

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

我建议你交叉编译新的CPU系列,而你的设备不支持它。

相关文章: