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

流过昼夜 提交于 2021-01-27 09:37:59

问题


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.

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;
}

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.

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

and the cross-compiled version outputs

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?


回答1:


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

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

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

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

Stretch default cross-compiler:

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

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

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



来源:https://stackoverflow.com/questions/39002372/cross-compiling-for-raspi-executing-the-programm-ends-in-segmentation-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!