目录
-
交叉编译
在一种计算机环境中运行的编译程序,能编译出在另外一种环境下运行的代码,我们就称这种编译器支持交叉编译。这个编译过程就叫交叉编译。
交叉编译这个概念的出现和流行是和嵌入式系统的广泛发展同步的。我们常用的计算机软件,都需要通过编译的方式,把使用高级计算机语言编写的代码(比如C代码)编译(compile)成计算机可以识别和执行的二进制代码。比如,我们在Windows平台上,可使用Visual C++开发环境,编写程序并编译成可执行程序。这种方式下,我们使用PC平台上的Windows工具开发针对Windows本身的可执行程序,这种编译过程称为native compilation,中文可理解为本机编译。然而,在进行嵌入式系统的开发时,运行程序的目标平台通常具有有限的存储空间和运算能力,比如常见的 ARM 平台,其一般的静态存储空间大概是16到32MB,而CPU的主频大概在100MHz到500MHz之间。这种情况下,在ARM平台上进行本机编译就不太可能了,这是因为一般的编译工具链(compilation tool chain)需要很大的存储空间,并需要很强的CPU运算能力。为了解决这个问题,交叉编译工具就应运而生了。通过交叉编译工具,我们就可以在CPU能力很强、存储空间足够的主机平台上(比如PC上)编译出针对其他平台的可执行程序。
要进行交叉编译,我们需要在主机平台上安装对应的交叉编译工具链(cross compilation tool chain),然后用这个交叉编译工具链编译我们的源代码,最终生成可在目标平台上运行的代码。
可通过buildroot和crosstool-ng这两个工具来建立我们自己的交叉编译器,其中buildroot基于uclibc制作交叉便器, 而crosstool-ng则基于glibc制作交叉编译器, crosstool-ng制作的交叉便器兼容性更好,如果内存足够建议选用这个。本文使用buildroot制作交叉编译器。
buildroot 是Linux平台上一个构建嵌入式Linux系统的框架。整个Buildroot是由Makefile脚本和Kconfig配置文件构成的。你可以和编译Linux内核一样,通过buildroot配置,menuconfig修改,编译出一个完整的可以直接烧写到机器上运行的Linux系统软件(包含boot、kernel、rootfs以及rootfs中的各种库和应用程序)。
前辈嘱咐: 制作交叉编译器别用ROOT制作, 否则会出现很多莫名其妙的错误!!!
首先选择存放路径, 我选择存放在 /opt/ 路径下, 因为不能使用root干活,所以需要给相关权限, 使用一下命令相应授权。
sudo chmod 777 /opt/
buildroot官网:https://buildroot.org, 我们到https://buildroot.org/downloads/选择我们要下载的buildroot版本:
选择 buildroot-2012.08.tar.bz2 这个版本,使用以下命令直接下载
wget https://buildroot.org/downloads/buildroot-2012.08.tar.bz2
下载完成后,解压,为防止乱码设置环境变量TERM类型为vt100,
tar -xjf buildroot-2012.08.tar.bz2
export TERM=vt100
前面部分基本没有什么问题, 从这步就开始出现各种问题,基本上是缺少一些包,缺啥就下啥就可以了,执行以下命令配置buildroot:
make menuconfig
错误Install ncurses (ncurses-devel) and try again.
Install ncurses (ncurses-devel) and try again.
解决方法, 使用以下命令安装包:
sudo apt-get install libncurses5-dev
错误configure: error: missing required tool: bison
解决方法, 使用以下命令安装包:
sudo apt-get install bison
错误configure: error: missing required tool: makeinfo
configure: error: missing required tool: makeinfo
解决方法, 使用以下命令安装包:
sudo apt-get install texinfo
错误configure: error: could not find GNU awk
configure: error: could not find GNU awk
解决方法, 使用以下命令安装包:
sudo apt-get install gawk
错误configure: error: could not find GNU automake >= 1.1
configure: error: could not find GNU automake >= 1.10
解决方法, 使用以下命令安装包:
sudo apt-get install automake
错误configure: error: could not find GNU libtool >= 1.5.26
configure: error: could not find GNU libtool >= 1.5.26
解决方法, 使用以下命令安装包:
sudo apt-get install libtool
错误configure: error: could not find curses header, required for the kconfig frontend
configure: error: could not find curses header, required for the kconfig frontend
解决方法, 使用以下命令安装包:
sudo apt-get install libncurses5-dev
-
buildroot相关配置
Build options配置
Toolchain配置
保存配置
配置好了之后记得点击 Save an Alternate Configguration File 保存
使用make命令开始安装, 此过程花费时间较长,我花了四十多分钟的时间,如果有包下载比较慢, 可通过win下载后放入虚拟机相应路径,这里是/opt/buildroot-2012.08/dl
sudo make
遇到出错问题
if [ xinfo = xinfo ]; then \
makeinfo --split-size=5000000 --no-split -I . -I /opt/buildroot-2012.08/output/toolchain/gcc-4.5.4/gcc/doc \
-I /opt/buildroot-2012.08/output/toolchain/gcc-4.5.4/gcc/doc/include -o doc/cpp.info /opt/buildroot-2012.08/output/toolchain/gcc-4.5.4/gcc/doc/cpp.texi; \
fi
/opt/buildroot-2012.08/output/toolchain/gcc-4.5.4/gcc/doc/cppopts.texi:763: @itemx must follow @item
make[2]: *** [doc/cpp.info] 错误 1
make[2]:正在离开目录 `/opt/buildroot-2012.08/output/toolchain/gcc-4.5.4-initial/gcc'
make[1]: *** [all-gcc] 错误 2
make[1]:正在离开目录 `/opt/buildroot-2012.08/output/toolchain/gcc-4.5.4-initial'
make: *** [/opt/buildroot-2012.08/output/toolchain/gcc-4.5.4-initial/.compiled] 错误 2
解决方法:将shell texinfo降级到4.13
使用以下命令安装texinfo4.13
wget http://ftp.gnu.org/gnu/texinfo/texinfo-4.13a.tar.gz
tar -zxvf texinfo-4.13a.tar.gz
cd texinfo-4.13
./configure
make
sudo make install
下载的文件都放在了/opt/buildroot-2012.08/dl 路径下
交叉编译器安装在:/opt/buildroot-2012.08/arm920t/usr/bin/ 路径下
编写hello.c, 使用arm-linux-gcc 编译和通过 gcc 编译查看结果:
-
FL2440校验
首先再SecureCRT上将编译好的可执行文件通过sz命令下载到本地windows系统上;在FL2440系统上通过tftp将本地Windows上的可执行文件下载到FL2440系统。
在下载之前先要检查FL2440是否和本地windows的有线网卡的IP地址处于同一网段内,简单方法就是通过IP地址与子网掩码做与运算,如果结果相同,则在同一网段内。
如上FL2440的IP地址为192.168.2.168,而我本地Windows的IP地址位192.168.1.2,就需要将我的FL2440的IP地址设为192.168.1.X就可以通过串口完成通信了。
设置FL2440IP地址
下载文件到FL2440
执行程序
出错 权限不允许,修改去权限