利用crosstool-ng自动化编译交叉编译环境(转)
原文地址:http://www.bootc.net/archives/2012/05/26/how-to-build-a-cross-compiler-for-your-raspberry-pi/
A cross compiler is a compiler that runs on one platform/architecture but generates binaries for another platform/architecture. With devices like the Raspberry Pi, where you really don’t have much CPU or memory to work with, if you’re doing any heavy compiling (like when working on the kernel) a cross compiler is the only way to go. For example, I build all my Raspberry Pi kernels on my nice Sandy Bridge Xeon E3 home server where they compile in only a fraction of the time they would on the Pi.
While there are a lot of different methods for building cross-compilers, by far the quickest and easiest is to use crosstool-ng. This is a set of scripts that bring up a menuconfig-like interface to choose your compiler settings, then goes off and downloads what it needs, patches it, configures it, builds it and installs it all for you. Here’s how to get started:
- Download crosstool-ng from the project web site. I’ve used version 1.15.2 as that was the latest when I wrote this.
- Unpack the tarball and
cd
into the unpacked directory, then run./configure --prefix=/opt/cross
. You can pick somewhere else instead of/opt/cross
but that’s where I like to keep it. - Run
make
andsudo make install
. - Make sure
/opt/cross/bin
is in your$PATH
.
Right, so that’s crosstool-ng installed and ready to build a toolchain for you. Next, to actually create your Raspberry Pi toolchain:
- Create a directory somewhere in your home directory that crosstool-ng will use as a staging ground. This will contain your toolchain configuration, downloaded files, and intermediary build results. This is not where your final toolchain will end up, and does take up quite a lot of space (3.5GB and up for me).
cd
into your chosen directory. - Run
ct-ng menuconfig
. You should see the ct-ng configuration menu. - Go into Paths and misc options. Enable Try features marked as EXPERIMENTAL. This is important.
- While you’re there you may want to change your Prefix directory. I like to put all my toolchains in
/opt/cross/x-tools/${CT_TARGET}
instead of${HOME}/x-tools/${CT_TARGET}
. - Go back to the main menu and select Target options.
- Change the Target architecture to arm. Leave Endianness set to Little endian and Bitness set to 32-bit.
- Go back to the main menu and select Operating system (skip Toolchain options, the defaults are fine).
- Change Target OS to linux.
- Go back to the main menu and select Binary utilities.
- Change binutils version to 2.21.1a or whichever is the latest that isn’t marked as experimental. Probably unnecessary but I prefer this.
- Go back to the main menu and select C compiler.
- Enable the Show Linaro versions (EXPERIMENTAL) option.
- In the gcc version field, choose the linaro-4.6-2012.04 (EXPERIMENTAL) compiler. You’re free to choose a different one but I know this one works well. I do recommend the Linaro versions over the vanilla ones for the RPi.
- All the other settings are fine left at their default values. Exit the configuration tool and save your changes.
- Run
ct-ng build
. - Go away and make a coffee, etc… This bit will take some time.
- You should end up with a freshly baked arm compiler ready for building kernels (and other things) for your Raspberry Pi in your Prefix directory you chose above.
- Just add your compiler directory to your
$PATH
and start compiling. If you used my preferred prefix directory, you would want to add/opt/cross/x-tools/arm-unknown-linux-gnueabi/bin
.