ARM的Linux平台的C/C++编译器有两个,一个是ARM自产的,一个是GNU的。
ARM自产的是收费的:
https://www.arm.com/ 打开官网,拉到网站最下面Support一列里选择Downloads。
在Download页面,下面有Other Popular Software and Tools,点击选择Arm Compiler for Linux。
打开的页面会提示,下载IED "Arm Allinea Studio", 里面就包含了“Arm Compiler for Linux”。
可以下载评估版,正常使用需要购买license。
而GNU是基于自由软件,基于GPL开源协议的,不收钱。
打开ARM的开发者网站,在顶部的菜单栏里选择Tools and Software,菜单展开,选择Open Source Software。
转到新页面,在顶部菜单栏选择Developer Tools,菜单展开,直接点击GNU Toolchain,进入新页面。
这里就能看到GNU的ARM Processors Toolchain:
Open source GNU tools for Arm processors: GCC, GDB, binutils, and libraries
分为两个,一个GNU-RM,表示Cortex-R和Cortex-M的芯片,这是bare metal的编译环境,比如Cortex-M系列的在单片机上直接执行程序的编译。
另一个是GNU-A,A-Profile,表示Cortex-A系列,基于Linux Kernel的应用程序的编译。
注意,GNU ARM Toolchain对C和C++的编译使用的命令是分开的。一个是arm-xxx-gcc,一个是arm-xxx-g++。
我们选择GNU-A,点击进去,再选择Download。
信息显示:
GNU Toolchain for the A-profile Architecture
Version 10.2-2020.11
Released: November 27, 2020
发现里面有这么一句:
This is the same toolchain that was previously distributed by Linaro.
就是说ARM GNU toolchain就是Linaro这个东东,去Linaro那里能找到最新的。
Linaro下载地址:
https://www.linaro.org/downloads/
-----------------------------------
我们直接在ARM官网来下载tool chain:
我们一般选择使用Linux平台,比如一般大家使用的是Ubuntu作为开发主机,普通PC,x86-64架构的。
所以选择“x86_64 Linux hosted cross compilers”,选择32-bit平台下面2个分支,因为用的是ARM Cortex-A7(ARMv7-A)的iMX6系列芯片:
- AArch32 bare-metal target (arm-none-eabi)
* gcc-arm-10.2-2020.11-x86_64-arm-none-eabi.tar.xz
- AArch32 target with hard float (arm-linux-none-gnueabihf)
* gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz
这里toolchain的命名也是遵循一定规则的:arch[-vendor][-os]-abi
- arch表示架构类型:arm, mips, x86 ...
- vendor表示tool chain的供应商,比如apple
- os表示操作系统:linux,none(bare metal)
- abi表示application binary interface约定:eabi, gnueabi, gnueabihf
EABI: embedded-application binary interface是一个嵌入式可执行程序的规范,包括数据类型、文件格式、寄存器使用、栈frame、函数指针传递等。
eabi表示ARM裸机可执行程序,gnueabi表示ARM上运行Linux系统的可执行程序。
abi就是普通的非嵌入式二进制程序接口。
所以,arm-none-eabi是裸机程序编译,arm-none-linux-gnueabihf是Linux系统上运行的程序。
那有个问题,arm-linux-gcc表示什么?
其实,这个就是省略了binary interface,而默认会编译出一个使用ABI的ARM架构的程序。
下载Linux版本,文件:gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz
$tar -xvf gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz
解压缩后,相关编译链接等工具就在bin路径下。
$./bin/arm-none-linux-gnueabihf-gcc helloworld.c
如果要在项目中使用这个工具链,请在makefile或cmake文件里设置相关toolchain路径。
或者将路径放到系统的可执行程序环境变量里也可以,配置文件: /home/username/.profile 或者/home/username/.bashrc (~/.bashrc)
在最后一行加上:
export PATH="/home/username/download/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/bin:$PATH"
或
export PATH="~/download/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/bin:$PATH"
------------------------------------------------
还有一种方法,在Ubuntu上使用apt-get直接安装:
$sudo apt-get update
$sudo apt-get install gcc-arm-linux-gnueabihf
这样直接安装使用也方便,只是版本可能不是最新的。
$arm-linux-gnueabihf-gcc helloworld.c
编译源文件。
$arm-linux-gnueabihf-gcc -v
查看版本。
使用gcc或者arm-linux-gnueabihf-gcc编译出的程序格式不同,执行环境不同。
gcc的在PC Linux上执行,arm的要在target board上执行。
$ file a.out
a.out: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=97fb9df8d6297b64fc43b971227c91f8e0de2bc5, for GNU/Linux 3.2.0, not stripped
$ file embedded
embedded: 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, with debug_info, not stripped
---------------------------------------------------
gnueabihf和gnueabi的区别,hf表示hard-float。
ARM处理器的浮点数运算有软件和硬件两种,硬件是带一个FPU,floating-point unit,用来计算浮点数。
有FPU的ARM处理器,使能FPU后就能加速浮点运算。
如果没有FPU就使用fplib一个软件库来做浮点计算,肯定会消耗更多的计算时间。
我们使用gnueabihf编译代码即可,如果芯片不支持硬件浮点,好像也一样能用。