7. 附录 - 建立树莓派交叉编译环境

自动安装器 https://github.com/openRPi/tools/tree/master/cross_complier_installer

建立交叉编译环境的关键是把**交叉编译器加入Linux系统路径**。

在基于Debian的Linux系统下,可以用包管理器

# apt-get install gcc-arm-linux-gnueabi

安装通用的ARM交叉编译器。

以此同时,树莓派官方也提供了4套交叉编译器:

  • arm-bcm2708-linux-gnueabi
  • arm-bcm2708hardfp-linux-gnueabi
  • gcc-linaro-arm-linux-gnueabihf-raspbian
  • gcc-linaro-arm-linux-gnueabihf-raspbian-x64 (运行于64位宿主机)

除了第四个运行于64位宿主机的交叉编译器外,前3套中任意一个都可以使用。

7.1. apt-get 安装通用交叉编译器

直接执行命令

# apt-get install gcc-arm-linux-gnueabi

成功执行后,ARM交叉编译器就安装好了。此时可执行命令 `arm-linux-gnueabi-gcc -v` 查看交叉编译器版本号,例如这样

concefly@ConceflyXBT:~$ arm-linux-gnueabi-gcc -v
使用内建 specs。
COLLECT_GCC=arm-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/arm-linux-gnueabi/4.7/lto-wrapper
目标:arm-linux-gnueabi
配置为:../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-12ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/arm-linux-gnueabi/include/c++/4.7.3 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-gnu-unique-object --disable-libmudflap --disable-libitm --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --enable-multilib --disable-sjlj-exceptions --with-arch=armv5t --with-float=soft --disable-werror --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=arm-linux-gnueabi --program-prefix=arm-linux-gnueabi- --includedir=/usr/arm-linux-gnueabi/include
线程模型:posix
gcc 版本 4.7.3 (Ubuntu/Linaro 4.7.3-12ubuntu1)

7.2. 安装树莓派官方交叉编译器

这里我们要手动配置一下目录。依照正文部分的讨论,我们把[交叉编译器](https://github.com/raspberrypi/tools)下载到 /home/YOUR_HOME/project/git/rpi_tools 目录下,然后把交叉编译器加入Linux系统路径。

$ cd ~/project/git
$ git clone https://github.com/raspberrypi/tools rpi_tools

这里我们选用arm-bcm2708-linux-gnueabi这套交叉编译器。

接下来打开 ~/.profile 文件。用户登录系统时,这个文件会被自动执行,因此可以用来配置一些登录自启动的程序。在最后一行加入:

PATH="/home/concefly/project/git/rpi_tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin:$PATH"
export PATH

保存,关闭文件,执行下面这条命令使PATH生效:

$ source ~/.profile

成功执行后,官方的交叉编译器就安装好了。此时可执行命令 arm-bcm2708-linux-gnueabi-gcc -v 查看交叉编译器版本号,例如这样

concefly@ConceflyXBT:~$ arm-bcm2708-linux-gnueabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-bcm2708-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/home/concefly/project/git/rpi_tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/../libexec/gcc/arm-bcm2708-linux-gnueabi/4.7.1/lto-wrapper
Target: arm-bcm2708-linux-gnueabi
Configured with: /home/extra/crosstool/stagingsf/.build/src/gcc-linaro-4.7-2012.04/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-bcm2708-linux-gnueabi --prefix=/home/dc4/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi --with-sysroot=/home/dc4/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/arm-bcm2708-linux-gnueabi/sysroot --enable-languages=c,c++ --with-cpu=arm1176jzf-s --with-tune=arm1176jzf-s --with-float=softfp --with-pkgversion='crosstool-NG 1.15.2' --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --with-gmp=/home/extra/crosstool/stagingsf/.build/arm-bcm2708-linux-gnueabi/buildtools --with-mpfr=/home/extra/crosstool/stagingsf/.build/arm-bcm2708-linux-gnueabi/buildtools --with-mpc=/home/extra/crosstool/stagingsf/.build/arm-bcm2708-linux-gnueabi/buildtools --with-ppl=/home/extra/crosstool/stagingsf/.build/arm-bcm2708-linux-gnueabi/buildtools --with-cloog=/home/extra/crosstool/stagingsf/.build/arm-bcm2708-linux-gnueabi/buildtools --with-libelf=/home/extra/crosstool/stagingsf/.build/arm-bcm2708-linux-gnueabi/buildtools --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm -L/home/extra/crosstool/stagingsf/.build/arm-bcm2708-linux-gnueabi/buildtools/lib -lpwl' --enable-threads=posix --enable-target-optspace --disable-nls --disable-multilib --with-local-prefix=/home/dc4/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/arm-bcm2708-linux-gnueabi/sysroot --enable-c99 --enable-long-long --with-float=softfp
Thread model: posix
gcc version 4.7.1 20120402 (prerelease) (crosstool-NG 1.15.2)