Satoshi’s Blog

Tips for IoT development.

Setup Cross Compile for Raspberry Pi

C/C++のプログラムをRaspberry Pi上でコンパイルするのはさすがに時間がかかるので、PC上でクロスコンパイルできる環境を作りたいと思います。

まず、クロスコンパイル用の環境としてLinux環境を用意します。今回は仮想環境(Virtualbox)上で動くUbuntu 14.04を使いました。

Raspberry Pi用のコンパイラは以下のGithubで公開されています。

ツールを適当なディレクトリにクローンします。

1
2
3
$ sudo mkdir /opt/raspberrypi
$ cd /opt/raspberrypi/
$ sudo git clone git://github.com/raspberrypi/tools.git

コンパイラが3種類入ってますが、gcc-linaro-arm-linux-gnueabihf-がRaspberry Piに最適化されたコンパイラです。

1
2
3
4
5
6
tools/
├── arm-bcm2708
│   ├── arm-bcm2708hardfp-linux-gnueabi
│   ├── arm-bcm2708-linux-gnueabi
│   ├── gcc-linaro-arm-linux-gnueabihf-raspbian
│   └── gcc-linaro-arm-linux-gnueabihf-raspbian-x64

gccの動作確認をしてみます。

1
2
3
4
5
6
7
8
9
$ cd /opt/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
$ ./arm-linux-gnueabihf-gcc -v
Using built-in specs.
COLLECT_GCC=./arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/opt/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/../libexec/gcc/arm-linux-gnueabihf/4.8.3/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: /home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/src/gcc-linaro-4.8-2014.03/configure --build=x86_64-build_unknown-linux-gnu --host=x86_64-build_unknown-linux-gnu --target=arm-linux-gnueabihf --prefix=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/install --with-sysroot=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-languages=c,c++,fortran --disable-multilib --enable-multiarch --with-arch=armv6 --with-tune=arm1176jz-s --with-fpu=vfp --with-float=hard --with-pkgversion='crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03' --with-bugurl=https://bugs.launchpad.net/gcc-linaro --enable-__cxa_atexit --enable-libmudflap --enable-libgomp --enable-libssp --with-gmp=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpfr=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpc=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-isl=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-cloog=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-libelf=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --enable-threads=posix --disable-libstdcxx-pch --enable-linker-build-id --enable-plugin --enable-gold --with-local-prefix=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-c99 --enable-long-long --with-float=hard
Thread model: posix
gcc version 4.8.3 20140303 (prerelease) (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03)

次にRaspberry Piのユーザランドを用意します。rsyncでRaspberry Piから取得します。

1
2
3
rsync -rlv --delete-after --safe-links \
     pi@$"Raspberry Pi's IP address":/{lib,usr} \
     /opt/raspberrypi/rootfs

Raspberry Pi側でapt-get updateや新しくパッケージをインストールする度に再取得する必要があるので、スクリプトにしておくよよいでしょう。

コンパイラにパスを通して簡単なプログラムをコンパイルしてみます。

1
2
3
4
5
6
7
8
#include <stdio.h>

int main(int argc, char* argv[])
{
     printf("Hello!\n");

     return 0;
}
1
2
3
$export RASPI_HOME="/opt/raspberrypi"
$export PATH=${RASPI_HOME}/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:$PATH
$ arm-linux-gnueabihf-gcc -Wall hello.c -o hello

fileコマンドでARMようの実行ファイルが生成されたことがわかります。

1
2
$ file hello
hello: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=038067c75c7098b2ef9838d9ceb931f3c4d9b97a, not stripped

ファイルをRaspberry Piにコピーします。

1
2
3
rsync -rlv --safe-links \
     ${HOME}/raspberrypi/ \
     pi@"Raspberry Pi's IP address":~/shared

Raspberry Piで動作することが確認できました。

1
2
pi@raspberrypi ~/shared $ ./hello
Hello!

References