博客
关于我
linux的系统移植——交叉编译器
阅读量:762 次
发布时间:2019-03-23

本文共 1683 字,大约阅读时间需要 5 分钟。

为什么要有交叉编译器?

一般来说,电脑是使用x86架构的,例如Intel或AMD的处理器,而单片机常常采用ARM架构。这样,当使用本地编译器(如gcc)编译程序时,生成的可执行文件是为x86架构设计的,无法在ARM架构的单片机上运行。为了能够在ARM架构的设备上运行程序,我们需要使用交叉编译器。交叉编译器能够生成针对目标架构(如ARM)的二进制代码,从而确保程序在不同平台上能够正常运行。

通过交叉编译器生成的程序文件属性可以通过以下命令查看:

file hello

运行结果通常会显示类似以下内容:

hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=bf1698069c3282a51cf380a29c30a8a9062e270e, for GNU/Linux 3.2.0, not stripped

安装交叉工具链

安装交叉工具链的过程相对简单,主要包括以下步骤:

  • 解压缩工具链至标准目录下,如 /usr/local/arm/
  • 添加交叉工具链的bin目录到系统环境变量PATH中。例如,编辑 /etc/environment 文件:
  • sudo edit /etc/environment

    在路径中添加:

    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/arm/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi/bin"

    然后重新应用变量:

    source /etc/environment

    如果需要使用绝对路径,可以直接在命令中指定工具链的路径:

    /usr/local/arm/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc hello.c -o hello

    使用交叉编译器

    使用交叉编译器可以通过以下两种方式:

  • 简单方法:在命令中添加交叉编译器前缀。例如:
  • arm-linux-gnueabi-gcc hello.c -o hello
    1. 绝对路径方法:使用交叉编译器的全路径名:
    2. /usr/local/arm/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc hello.c -o hello

      添加32位库

      在64位Linux系统上使用交叉编译器时,可能会遇到缺少32位库的问题。此时需要安装相应的32位依赖项:

      • Ubuntu 12.04及 earlier 版本:
      sudo apt-get install ia32-libs
      • Ubuntu 12.04及 later 版本:
      sudo apt-get install libc6:i386sudo apt-get install lib32z1

      查看交叉编译器版本和程序架构

      为了确认交叉编译器版本,可以执行:

      arm-linux-gnueabi-gcc -v

      编译并查看生成的可执行文件架构:

      file hello

      运行结果通常会显示如下的信息:

      hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux2.6.32, not stripped

      通过以上方法,您可以成功地使用交叉编译器在不同架构上运行程序,同时确保依赖的32位库已正确安装。

    转载地址:http://nzlzk.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named 'pandads'
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>