Friday, December 19, 2014

Cross-Compiling for the Raspberry Pi

It's not cost-efficient to develop and compile on the Pi itself. Instead, you would probably to develop and compile on your multi-core development system, then transfer the binary over to the Pi and test it there. To do this, you need a set of tools, a toolchain,

The following steps describe how to create a no-compromises Raspberry Pi toolchain on Ubuntu 12.04 using Crosstool-NG.

1. Install dependencies

Run the following command to install the required packages:
$ sudo apt-get install gawk bison flex gperf cvs texinfo automake libtool ncurses-dev g++ subversion python-dev libexpat1-dev cmake
2. Download, build, and install Crosstool-NG

Crosstool-NG isn't available in the standard Ubuntu repositories. Run the following commands to download, build, and install it:
$ pushd ~/raspberry_pi/build
$ curl -O http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.18.0.tar.bz2
$ tar xf crosstool-ng-1.18.0.tar.bz2
$ cd crosstool-ng-1.18.0
$ ./configure --enable-local && make && make install
$ export PATH=`pwd`:$PATH
$ popd
3. Configure, build, and install the toolchain

Crosstool-NG comes with a tool called menuconfig that makes it easy to configure how the toolchain should be built. Run the following commands to launch menuconfig, then follow the sub-sections below to configure the toolchain build parameters:
$ pushd ~/raspberry_pi/build/toolchain
$ ct-ng menuconfig
Setup Path Options
  • Navigate to Paths and misc options.
  • Enable Try features marked as EXPERIMENTAL.
  • Set Prefix directory to ${HOME}/raspberry_pi/toolchains/${CT_TARGET}.
  • Enable Use a mirror.
  • Set Number of parallel jobs to twice the number of HW threads.
Setup Target Options
  • Navigate to Target options.
  • Set Target Architecture to arm.
  • Leave Endianness as Little and Bitness as 32-bit.
  • Set append ‘hf' to the tuple (EXPERIMENTAL).
Setup Operating System Options
  • Navigate to Operating System.
  • Set Target OS to Linux.
Setup Binary Utility Options
  • Navigate to Binary utilities.
  • Set Binutils to 2.22.
Setup Compiler Options
  • Navigate to C compiler.
  • Enable Show Linaro versions.
  • Enable C++ support.
C-library
  • Set eglibc version to 2_13.
Setup Debug Options
  • Navigate to Debug facilities.
  • Enable gdb, then press [Enter] to configure additional GDB settings.
    • Enable Build a static cross gdb.
    • Set Cross-gdb extra config to --with-expat.
  • Enable ltrace.
  • Enable strace.
Exit and confirm to save the configuration file returning to your shell.

4. Build the toolchain

We are now ready to build and install the toolchain. Start the build by running the following commands:
$ ct-ng build
$ popd 
If the build was successful, the toolchain will be located at ~/raspberry_pi/toolchains/arm-unknown-linux-gnueabihf/. All the tools (gcc, ld, gdb, etc) are located in the bin/ directory of the toolchain with the name of the toolchain prefixed.

Run the following command to add the toolchain binaries to the PATH environment variable:
$ pushd ~/raspberry_pi/toolchains/arm-unknown-linux-gnueabihf/bin
$ export PATH=`pwd`:$PATH
$ popd

No comments:

Post a Comment