【问题标题】:Cross Compiling for Raspi - Executing the programm ends in "Segmentation fault"Raspberry 的交叉编译 - 执行程序以“分段错误”结束
【发布时间】:2016-08-17 16:57:07
【问题描述】:

我有一个自己编写的程序,我也想从我的 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的输出是Segmentation 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

和交叉编译的版本输出

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

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

【问题讨论】:

  • 你能把 ldd hello_world 的输出放上去吗
  • 在你的 Pi 上,尝试 gdb hello_world,然后运行程序并观察它的段错误。然后使用 bt 获取回溯,它应该提供更多信息。
  • 上raspi编译后的输出:pi@raspberrypi:~ $ ldd hello_world /usr/lib/arm-linux-gnueabihf/libarmmem.so (0xb6f40000) libstdc++.so.6 =&gt; /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0xb6e52000) libm.so.6 =&gt; /lib/arm-linux-gnueabihf/libm.so.6 (0xb6dd7000) libgcc_s.so.1 =&gt; /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0xb6daa000) libc.so.6 =&gt; /lib/arm-linux-gnueabihf/libc.so.6 (0xb6c69000) /lib/ld-linux-armhf.so.3 (0x7f57c000)
  • 和交叉编译:pi@raspberrypi:~ $ ldd hello_world_cc /usr/lib/arm-linux-gnueabihf/libarmmem.so (0xb6f1f000) libstdc++.so.6 =&gt; /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0xb6e31000) libgcc_s.so.1 =&gt; /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0xb6e04000) libc.so.6 =&gt; /lib/arm-linux-gnueabihf/libc.so.6 (0xb6cc3000) libm.so.6 =&gt; /lib/arm-linux-gnueabihf/libm.so.6 (0xb6c48000) /lib/ld-linux-armhf.so.3 (0x7f587000)
  • @FrancescaNannizzi gdb 的输出:pi@raspberrypi:~ $ gdb hello_world_cc ... This GDB was configured as "arm-linux-gnueabihf". ... (gdb) run Starting program: /home/pi/hello_world_cc Program received signal SIGSEGV, Segmentation fault. 0x0005f7e2 in ?? () (gdb) bt #0 0x0005f7e2 in ?? () #1 0x0001062e in _start () (gdb)

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


【解决方案1】:

工具链编译器 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 系列进行交叉编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2016-12-24
    • 2012-06-28
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多