Skip to content

Cross compiling for Raspberry Pi

Antoine Villeret edited this page Jan 4, 2017 · 15 revisions

Cross Compiling ofnode for Raspberry Pi

Requirements:

I'm using Ubuntu 16.10

Preparing the Raspberry Pi

1. Copy RPi image to SD

Use Ubuntu disk utility if you're lazy like me. You can do it with dd too, there are plenty of command line example on the net but you should end up with something like :

$ sudo dd bs=1m if=2016-11-25-raspbian-jessie-lite.img of=/dev/rdisk2

2. Connect the Raspberry Pi directly to your host network

You can either connect the RPi directly to your computer and share a Wifi connection via ethernet adapter, or connect it to the same router your computer is connected to.

3. Configure your RPi

On 2016-11-25-raspbian-jessie-lite.img ssh is not enabled by default. The simplest way to set up is to plug a screen, a keyboard and a power cable to the RPi then on the Pi terminal :

$ sudo raspi-config

In the raspi-config menu, select Option 1 Expand Filesystem, then go to 5 Interfacing Options and choose P2 SSH Enable/Disable remote command line access to your Pi using SSH (this menu position may change according to your raspi-config version) then enable the SSH service. You can also change Keyboard layout, localization, time zone, etc. and reboot.

Now you connect through SSH to the RPi with :

$ ssh pi@raspberrypi.local

And use this session to enter the following command line instructions.

4. Install OpenFrameworks dependencies into Raspberry Pi

Now, update the dependencies required when cross-compiling by downloading and running raspbian.sh.

pi@raspberrypi:~ $ wget  https://raw.githubusercontent.com/ofnode/of/master/dev/install/linux/raspbian.sh
pi@raspberrypi:~ $ chmod +x raspbian.sh
pi@raspberrypi:~ $ sudo ./raspbian.sh

5. Make an new image file from the existing and updated Raspberry Pi

Shutdown the Raspberry Pi with :

pi@raspberrypi:~ $ sudo shutdown -P now

Then remove the SD card from the Raspberry Pi, insert the SD card in your host and use dd to make an new image file.

$ df # and identify where is your SD card, note both first and last item in the line
Sys. de fichiers blocs de 1K   Utilisé Disponible Uti% Monté sur
/dev/sdc2            7513804   6000140    1158168  84% /media/antoine/13d368bf-6dbf-4751-8ba1-88bed06bef77
/dev/sdc1              57288     20384      36904  36% /media/antoine/boot
$ umount /media/antoine/boot /media/antoine/13d368bf-6dbf-4751-8ba1-88bed06bef77
$ sudo dd bs=1m if=/dev/sdc of=raspbian-jessie-lite+of+ofnode_dependency.img # you could try to increase bs value but bbe careful this could cause read/write error and then you'll end up with a corrupted image.

Or use Linux disk utility GUI to do the same. I am using raspbian-jessie-lite+of+ofnode_dependency.img as the image name.

6. Mount image in read/write mode

Now we need to mount the .img but default image loader (when you double click it) mount in read only mode and we need to fix dead symlink.

Get the partition table with :

$ fdisk -lu raspbian-jessie-lite+of+ofnode_dependency.img
Périphérique                                                                                Amorçage  Start     Fin Secteurs  Size Id Type
raspbian-jessie-lite+of+ofnode_dependency.img1            8192  137215   129024   63M  c W95 FAT32 (LBA)
raspbian-jessie-lite+of+ofnode_dependency.img2          137216 7774207  7636992  3,7G 83 Linux

Then get the start offset of the second partition (The Linux one) and multiply it by 512 to get an offset in octet rather than in sector. Here it is 137216 * 512 = 70254592. Then mount the partition with read/write permission :

$ mkdir /tmp/rpi/root
$ sudo mount -o loop,offset=70254592,rw,sync  raspbian-jessie-lite+of+ofnode_dependency.img  /tmp/rpi/root

7. Fix dead link

Many symbolic links in Linux system refered to absolute path with cause error when mounting the image in a folder, like we do. So we need to fix the links with :

$ cd /tmp/rpi/root
$ sudo ln -s ../../../lib/arm-linux-gnueabihf/libudev.so.1.5.0 libudev.so
$ sudo ln -s ../../../lib/arm-linux-gnueabihf/libglib-2.0.so.0 libglib-2.0.so
$ sudo ln -s ../../../lib/arm-linux-gnueabihf/libz.so.1 libz.so

Prepare host

Get a cross-compiler

In Ubuntu 16.10 you can get arm-linux-gnueabihf-gcc-4.9 and arm-linux-gnueabihf-g++-4.9 with :

$ sudo apt install gcc-4.9-arm-linux-gnueabihf  g++-4.9-arm-linux-gnueabihf

Download ofnode

If you don't have it already :

$ git clone https://github.com/ofnode/of.git

Configure and build

$ mkdir build-of-rpi-debug 
$ cd build-of-rpi-debug 
$ cmake ../of -DCMAKE_TOOLCHAIN_FILE=${PWD}/../of/dev/arm-linux-gnueabihf.cmake -DRPI_ROOT_PATH='/tmp/rpi/root' -DOF_PLATFORM=armv7 -G Ninja 

Then you're ready to build :

$ ninja

Take a reste and a coffee :-)

Clone this wiki locally