This is a short post about how to setup a chroot to cross-compile for the Raspberry Pi without installing loads of libraries on your host machine.

You will need 4 things:

  1. An image of the ubuntu distribution you want to use
  2. The default chroot creation script
  3. The update script to setup development
  4. The start script that will get you inside the chroot

I have all of the script in my GitHub repository. A good start would be to create a location for it:

cd /opt
sudo mkdir raspi
sudo chown $(whoami):$(whoami) raspi

First download an Ubuntu image from here. Then from the Github repository sub-folder, download the 3 files:
1. create-raspi.sh
2. dev-tools.sh
3. start-raspi.sh

You’ll need to run the create script first:

sudo ./create-raspi.sh ubuntu-20.04.1-preinstalled-server-arm64+raspi.img.xz

It will take some time then you’ll have a new chroot-dir directory with a base image.

Before we get to chroot, you might want to edit the start-raspi.sh file for the mapping to your source code directoy:

# Line 9 and 10
mkdir -p chroot-dir/usr/local/src/deadite
mount -o bind ../deadite/ chroot-dir/usr/local/src/deadite
# Line 23
umount chroot-dir/usr/local/src/deadite

Update the chroot-dir/usr/local/src/deadite and ../deadite/ to match your paths.

Next get in the chroot by running the command:

sudo ./start-raspi.sh

Within the chroot environment, you’ll need to run the dev-tools.sh script:

./dev-tools.sh

That’s it. Now you can cross compile with the gcc command for RPi. The generated libraries and executables will be able to run on your RPi directly.

One Reply to “Cross-compilation for Raspberry Pi using chroot”

Comments are closed.