(补充)交叉编译器安装配置

交叉编译器的安装和使用

环境Ubuntu 20.04 LTS

前面教程提到了交叉编译器,忘了写具体的安装和配置方式,在这里补充一下

gcc编译器编译的程序只能在x86环境下运行,而不能在arm上运行
gcc运行方式

直接写一个程序hello.c然后输入命令

sudo gcc hello.c	#编译器会自动生成a.out可执行文件
sudo gcc hello.c -o hello	#编译成hello可执行文件

./a.out 或者 ./hello 运行程序
file a.out   #查看文件运行环境信息
pymeia@akiyamayusuke:~/test$ 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]=0095ffb668ed42c35e0ee29f98209b8f72724e40, for GNU/Linux 3.2.0, not stripped

可以看到显示运行环境为x86

交叉编译器安装

首先使用命令查看可安装版本

sudo apt list gcc-arm*

然后从中选择一个安装即可

安装好后找到环境路径,如默认的安装环境路径为

/usr/arm-linux-gnueabi/bin

修改全局环境变量

sudo gedit /etc/profile
#在文件的最后添加如下环境命令
export PATH=$PATH:/usr/arm-linux-gnueabi/bin

用交叉编译器编译hello.c

sudo arm-linux-gnueabi-gcc hello.c -o armhello	#使用gnueabi编译
file armhello	#查看运行环境为ARM

如果安装的是gnueabihf-gcc 那么上述命令相应位置要改成gnueabihf

posted @ 2021-06-01 12:17  水鸽  阅读(159)  评论(0编辑  收藏  举报