How to Build a GCC 4.7.2 Cross Compile Toolchain for Raspbian ARM on OS X - abencomo/toolchain GitHub Wiki
The Raspberry Pi GitHub project already provides cross‐compilation toolchains, but they are only for Linux. If you use a Mac OS X system for development, then you have to build your own C cross compiler.
This page describes how you can establish a fully featured cross‐compilation environment for building code for the Raspberry Pi 2 Model B (ARM Cortex-A7) platform using your OS X desktop or laptop.
While all of the steps in this article work at the time it was created, it is entirely possible that some steps will change as updates are performed on the OS X and Raspbian releases or the crosstool-ng versions. Therefore, check this page for updates.
You will need Hombrew to install some dependencies. If you haven’t already installed it, then you can run the command below:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
I've had problems with the above command in the past so if that is also your case, then you can try this one instead:
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install | ruby
brew tap homebrew/versions
brew install gcc47
brew install autoconf
brew install crosstool-ng
brew tap homebrew/dupes
brew install grep --with-default-names
brew install gettext
brew link --force gettext
brew install ncurses
brew link --force ncurses
You will also need XCode command line tools:
xcode-select --install
nano ~/.bash_profile
export PATH=/usr/local/bin/:$PATH
Press control
+ x
to save the file and then source it:
source ~/.bash_profile
Create a link to it:
cd /usr/local/bin/
ln -s gcc-4.7 gcc
Verify that 4.7 is now the shell default version:
gcc --version
Edit paths.sh and change the grep line to:
chmod u+w /usr/local/Cellar/crosstool-ng/1.21.0/lib/ct-ng.1.21.0/paths.sh
nano paths.sh
export grep="/usr/local/bin/grep"
From Terminal, run this command:
mkdir xtools-images
Then, open up Disk Utility and click on the New Image button.
Add a disk at least 6GB in size. This will house all the source code and object files when all said and done.
The next disk can be exactly the same but only 500MB in size. (When fully compiled and compressed everything turned out to be around 107MB)
If don't want to use Disk Utility, then you can run these 2 commands from the terminal:
hdiutil create -size 6g -attach -type SPARSE -fs 'Case-sensitive Journaled HFS+' -volname xtools-build-env xtools-build-env
hdiutil create -size 500m -attach -type SPARSE -fs 'Case-sensitive Journaled HFS+' -volname rpi-xtools rpi-xtools
From Terminal, run these commands:
chmod 777 /Volumes/rpi-xtools/
chmod 777 /Volumes/rpi-xtools/.Trashes/
cd /Volumes/xtools-build-env/
mkdir src
Download this .config file and copy it into the /Volumes/xtools-build-env/ directory as .config (make sure it has the front .dot after it was downloaded).
The Raspberry Pi 2 has a Cortex-A7 processor with NEON optimization and supports floating-point hardware. Therefore, the .config file is setup for -mcpu=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard
Notice that using hard floats means that when you are working with floating‐point numbers on the RPi you are using the on‐chip floating‐point unit (FPU); which and the operations are much faster than soft float operations.
Now, launch the crosstool configuration application:
ct-ng menuconfig
Make sure that under "Paths and misc options", the "Stop after extracting tarballs" option is SELECTED, as shown below:
The first build attend opens a lot of files in parallel so you have to set this in the Terminal:
ulimit -n 4096
ct-ng build
The build command will stop after extracting all the sources.
Download this Makefile file and copy it into the /Volumes/xtools-build-env/.build/src/glibc-2.21/sunrpc/ directory. Then, download this types.h file and copy it into the /Volumes/xtools-build-env/.build/src/glibc-2.21/sunrpc/rpc/ directory.
Launch ct-ng menuconfig again and unselect the "Stop after extracting tarballs" option under "Paths and misc options". Open up:
ct-ng menuconfig
ct-ng build
This will take 20-30 minutes to build, so you can have a cup of coffee or tea!
Run this command from terminal:
hdiutil attach rpi-xtools.dmg
Create a test.c file in your OS X system:
-
cat > test.c
#include <stdio.h>
int main() { printf("Hello, world!\n"); return 0; }
(Hit ctrl-d to escape)
/Volumes/rpi-xtools/arm-unknown-linux-gnueabi-gcc -o test test.c
Copy test over to your Raspberry Pi.
rsync -rtzh --delete test pi@raspberrypi:/home/pi
Then ssh in and run the test executable
ssh pi@raspberrypi '/home/pi/test'