嵌入式Linux 交叉编译工具链

前言

这是前2篇:

.c之类的源码不能直接运行, 需要编译链接生成可执行文件, 才能执行. 这就需要工具链(toolchain). Embedded Systems Programming Hello World for ARM - 2020 这篇讲的很好, 摘录部分如下:

Toolchain consists of a compiler, linker, assembler, and a debugger.
工具链是编程工具的集合, 包含编译, 链接器, 汇编器, 调试器.
在这里插入图片描述

PC可以直接编写源代码, 然后装个编译器(如gcc)就能生产出自己可以执行的文件, 这种叫native compilation. 如在x86编写代码, 在x86编译, 在x86运行.

但嵌入式设备不一定行, 限于空间, 算力, 一般无力或者不方便直接编写/编译.交叉编译(cross-compilation)是在一个平台上生成另一个平台上的可执行代码. 可以在主机安装交叉编译工具链编译生成可执行文件, 然后再把可执行文件放到嵌入式设备运行. 如在x64架构的Linux机器上编译ARM平台, 需要 Linux-based ARM-targeting cross compiler.

Toolchains have a loose name convention like arch [-vendor] [-os] - eabi
arch - refers to target architecture (which in our case is ARM)
vendor - refers to toolchain supplier
os - refers to the target operating system
eabi - refers to Embedded Application Binary Interface

some illustrations as follows :

arm-none-eabi - This tool chain targets for ARM architecture, has no vendor, does not target an operating system and complies with the ARM EABI.
arm-none-linux-gnueabi - This toolchain targets the ARM architecture, has no vendor, creates binaries that run on the Linux operating system, and uses the GNU EABI. It is used to target ARM-based Linux systems.
arm-linux-gcc - This is actually binary for gcc which produces objects for ARM architecture to be run on Linux with default configuration (abi) provided by toolchain.
arm-eabi - Android ARM compiler
i686-apple-darwin10-gcc-4.2.1 - This toolchain targets the Intel i686 architecture, the vendor is Apple, and the target OS is Darwin version 10.

i.mx6ull是ARM的Cortex-A7架构, 可以用Linaro公司产的arm-linux-gnueabihf, 大概是The GNU Embedded Application Binary Interface for armhf architecture的意思, 可参见 各版本arm-gcc区别与安装或者交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别.

安装arm-linux-gnueabihf

系统是Ubuntu18, 常理来讲, 只需要sudo apt-get install gcc-arm-linux-gnueabihf一条命令安装最新的即可, 但原子的手册上不推荐最新的7.x, 说是发现编译后的Uboot无法运行, 推荐安装4.9版本, 下载地址, 因为是64位的系统, 需要下载如图:
在这里插入图片描述
之后步骤:

  • 创建目录: sudo mkdir /usr/local/arm
  • 复制下载的压缩包到创建的目录: sudo cp gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz /usr/local/arm
  • 切换到目录cd /usr/local/arm, 解压: sudo tar vxf gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf
  • 修改环境变量: sudo vim /etc/profile, 末尾添加 export PATH=$PATH:/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin:
    在这里插入图片描述
  • 保存退出, 如原子手册, 还要安装库: sudo apt-get install lsb-core lib32stdc++6
  • 重启Ubuntu. 然后用arm-linux-gnueabihf-gcc -v查看版本号, 最后应该显示gcc version 4.9.4 (Linaro GCC 4.9-2017.01)

检验

上篇用NFS共享了/home/karoto/linux/nfs目录, 在这个目录里, 编写main.c文件:

#include <stdio.h>

int main()
{
    printf("hello, world!\r\n");
    return 0;
}

先用gcc: gcc -o main main.c, 运行./main, 输出hello, world!, 然后在嵌入式平台运行, 输出:
在这里插入图片描述
运行不了, 用file main辨识一下main的文件类型:
在这里插入图片描述
怪不得在32bit A7上不能运行, 需要用上小节的交叉编译: arm-linux-gnueabihf-gcc -o main1 main.c

file main1查看:
在这里插入图片描述
运行./main1, 在服务器的Ubuntu18上出错, 然后在嵌入式平台运行, 输出正常:
在这里插入图片描述

微信公众号

欢迎扫描关注我的微信公众号, 及时获取最新文章:
在这里插入图片描述

发布了203 篇原创文章 · 获赞 105 · 访问量 38万+

猜你喜欢

转载自blog.csdn.net/weifengdq/article/details/104638188