Raspi 的交叉编译 - 执行程序以“分段错误"结束 [英] Cross Compiling for Raspi - Executing the programm ends in "Segmentation fault"

查看:16
本文介绍了Raspi 的交叉编译 - 执行程序以“分段错误"结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a self-written programm which I want to build also for Raspberry Pi from my x86 machine. I'm using eclipse generated makefiles and cannot change this thing.

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

I've read this tutorial for CC for raspi : Hackaday-Link. Because raspi also have installed gcc version 4.9, I also try it with this version of the cross compiler. The problem also exisits with this hello world programm:

#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 时没有输出.

When compiling and running it directly on the raspi, the output is hello world!. OK, thats fine. But when cross-compiling it with version 4.9 of arm-linux-gnueabihf-g++-4.9, then scp it to the raspi, make it executable and run it, the output of ./hello_world is Segmentation fault. When executing sudo ./hello_world there is no output.

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

I tried to get some information about the files and see that the locally on the raspi compiled program outputed:

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

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

Can anyone tell me what the problem is and how to solve it?

推荐答案

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

Toolchain compiler arm-linux-gnueabihf-gcc can have different default parameters which one can get running:

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

安装在 Raspberry Stretch 上的编译器(我只会留下必要的信息):

Compiler installed on Raspberry Stretch (I will leave essential information only):

<代码>-march= armv6-marm [启用]-mfloat-abi=硬-mfp16-format= 无-mfpu=vfp

Stretch 默认交叉编译器:

Stretch default cross-compiler:

<代码>-march= armv7-a-marm [禁用]-mfloat-abi=硬-mfp16-format= 无-mfpu= vfpv3-d16

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

Now you see the difference in architecture. So to sompile with cross-compiler one will need to set march to match your desired CPU. Also note that Debian cross compiler by default emits Thumb code and Raspberry Stretch emits ARM code

我建议您在您的设备不支持的情况下为较新的 CPU 系列进行交叉编译.

I suggest you're cross-compiling for newer CPU family while your device doesn't support it.

这篇关于Raspi 的交叉编译 - 执行程序以“分段错误"结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆