Build Cross Compile Tools for Raspberry Pi (Raspbian Stretch) on macOS

This guide is based on this article.

Written and tested with:

  • Raspberry Pi 3 B+
    • Raspbian Stretch (GCC 6.3.0 20170516)
  • macOS 10.13.3
    • clang+llvm 6.0.0
    • binutils 2.30

Create a SDK directory

Create a SDK directory for not messing other things up:

$ mkdir -p raspbian-sdk/{prebuilt,sysroot}

Download / Install Tools


$ wget
$ tar -xzvf clang+llvm-6.0.0-x86_64-apple-darwin.tar.xz -C "raspbian-sdk/prebuilt" --strip-components=1


$ wget
$ tar -xzvf binutils-2.30.tar.xz
$ cd binutils-2.30
$ brew install coreutils
$ ./configure --prefix="`/usr/local/bin/realpath ../raspbian-sdk/prebuilt`" \
	--target=arm-linux-gnueabihf \
	--enable-gold=yes \
	--enable-ld=yes \
	--enable-targets=arm-linux-gnueabihf \
	--enable-multilib \
	--enable-interwork \
	--disable-werror \
$ make && make install

Now come back to the initial directory:

$ cd ..

Copy Header/Library Files from Raspberry Pi

Let’s copy header and library files from Raspberry Pi over the network with rsync:

$ brew install rsync
$ /usr/local/bin/rsync -rzLR --safe-links \
	[email protected]:/usr/lib/arm-linux-gnueabihf \
	[email protected]:/usr/lib/gcc/arm-linux-gnueabihf \
	[email protected]:/usr/include \
	[email protected]:/lib/arm-linux-gnueabihf \

Create a Wrapper Shell Script for Cross Compiling

Open a new file,

$ vi raspbian-sdk/prebuilt/bin/arm-linux-gnueabihf-clang

and fill it with following lines:

BASE=$(dirname $0)
	"${BASE}/clang" --target=${TARGET} \
		--sysroot="${SYSROOT}" \
		-isysroot "${SYSROOT}" \
		--gcc-toolchain="${BASE}" \
		"[email protected]"

then grant executable permission to it:

$ chmod +x raspbian-sdk/prebuilt/bin/arm-linux-gnueabihf-clang

Test Build

Create a hello.c file with following content:

#include <stdio.h>

int main(int argc, char** argv) {
	printf("Hello, world\n");
	return 0;

then build it with the shell script:

$ raspbian-sdk/prebuilt/bin/arm-linux-gnueabihf-clang -o hello hello.c

If nothing goes wrong, a file named hello would be generated.

Upload it to the Raspberry Pi and run with:

[email protected] $ ./hello

then it will print out:

Hello, world

Well done!

If any error occures while building, try again with -v option to see what went wrong:

$ raspbian-sdk/prebuilt/bin/arm-linux-gnueabihf-clang -o hello hello.c -v

You may have to change the version number of COMPILER_PATH in your shell script, or install some packages on Raspberry Pi and copy header/library files again.


Building large projects or binaries on Raspberry Pi is painful; it often goes unresponsive and even crashes sometimes.

With this cross compilation tools, I will try building TensorFlow or Haskell packages like ghc-mod.

Will post about it if I see some progress :-)