c++ - 树莓派交叉编译——执行程序结束于 "Segmentation fault"

标签 c++ linux compilation raspberry-pi cross-compiling

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

我已经阅读了 raspi 的 CC 教程:Hackaday-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的输出段错误。执行 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

和交叉编译版本输出

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 上的编译器(我只留下必要的信息):

-行军= armv6 -marm [启用] -mfloat-abi=硬 -mfp16-格式=无 -mfpu=vfp

Stretch 默认交叉编译器:

-行军= armv7-a -marm [禁用] -mfloat-abi=硬 -mfp16-格式=无 -mfpu=vfpv3-d16

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

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

关于c++ - 树莓派交叉编译——执行程序结束于 "Segmentation fault",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002372/

相关文章:

C++ Boost 对象序列化 - 定期保存以保护数据

c++ - 语言标准中的 "as if"

linux - 可执行文件不是用 Qt 制作的

c++ - 如何使用 g++ 编译 openmp

c++ - 升级 gcc 编译器/其他修复以实现向后兼容

visual-studio - 如何在 Visual Studio 2015(对于 C++)中仅禁用 SIMD 自动矢量化优化?

c++ - 在 QtCreator 中使用 cin

c++ - 如果在 Tesseract API 中安装了某种语言,如何从代码中检查?

linux - 修剪命令行 unix 上的输出

java - 是否有一种独立于平台的方式来使用 gradle 设置文件权限?