• 解决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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    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到树莓派执行,都可以正常执行,问题解决。

  • 相关阅读:
    Spring之AOP思想
    Android笔记
    JS面向对象---原型链继承
    月薪2w的前端工程师,必盘的实战项目
    【操作系统】2009年408真题第 46 题
    QQ怎么远程控制自己的电脑
    GICv3和GICv4虚拟化
    k8s概述
    【JavaWeb】第七章 Tomcat
    Nginx优化与防盗链
  • 原文地址:https://blog.csdn.net/weixin_51760563/article/details/127795535