解决arm-none-eabi-gcc交叉编译helloworld程序segmentation fault 错误

目标:在x86_64主机上(windows 10或ubuntu 20.04)上交叉编译arm架构的helloworld程序,然后通过scp将编译完的可执行文件传给树莓派4B,在树莓派上执行该程序。(树莓派运行的是官方32位Linux系统。)

1、执行arm-none-eabi-gcc test.c -o test报错

/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-exit.o): in function `exit':
exit.c:(.text.exit+0x2c): undefined reference to `_exit'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): in function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0x18): undefined reference to `_sbrk'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-writer.o): in function `_write_r':
writer.c:(.text._write_r+0x24): undefined reference to `_write'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-closer.o): in function `_close_r':
closer.c:(.text._close_r+0x18): undefined reference to `_close'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-fstatr.o): in function `_fstat_r':
fstatr.c:(.text._fstat_r+0x1c): undefined reference to `_fstat'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-isattyr.o): in function `_isatty_r':
isattyr.c:(.text._isatty_r+0x18): undefined reference to `_isatty'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-lseekr.o): in function `_lseek_r':
lseekr.c:(.text._lseek_r+0x24): undefined reference to `_lseek'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-readr.o): in function `_read_r':
readr.c:(.text._read_r+0x24): undefined reference to `_read'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-abort.o): in function `abort':
abort.c:(.text.abort+0x10): undefined reference to `_exit'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-signalr.o): in function `_kill_r':
signalr.c:(.text._kill_r+0x1c): undefined reference to `_kill'
/home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /home/sunch/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-signalr.o): in function `_getpid_r':
signalr.c:(.text._getpid_r+0x4): undefined reference to `_getpid'
collect2: error: ld returned 1 exit status

2、在网上查找相关问题,解决方式是编译时添加--specs=nosys.specs

参考:https://www.freesion.com/article/1071345305/

所以编译命令改为 arm-none-eabi-gcc --specs=nosys.specs hello.c -o hello

这次可以成功编译,然后将生成的可执行文件scp到树莓派执行,报错:Segmentation fault

请添加图片描述

网上找到的解决方法都不适用:
1、segmentation fault 错误(运行一个简单的helloworld程序)
2、解决交叉编译产生的程序放到目标板上运行时出现Segmentation fault (core dumped)
3、嵌入式linux中遇到的segmentation fault问题

下面的网页解释了一部分原因,但是并没有解决问题:

https://www.coder.work/article/1516675

灵机一动,突然想到可能交叉编译器使用错误,arm-none-eabi从名字上就可以看出来是专门编译裸机程序的,可能不适合编译Linux应用程序。上网一查,果然,有专门交叉编译Linux应用程序的arm-linux-gnueabi-gcc。

3、解决方法:安装arm-linux-gnueabi-gcc工具链

在ubuntu上执行sudo apt install gcc-arm-linux-gnueabi就可以,在windows10上要下载:gcc-arm-10.3-2021.07-aarch64-arm-none-linux-gnueabihf.tar.xz (如果目标系统是arm32的),下载链接:https://developer.arm.com/downloads/-/gnu-a

使用新安装的交叉编译工具链重新编译程序,然后scp到树莓派执行,都可以正常执行,问题解决。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值