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:
- An image of the ubuntu distribution you want to use
- The default chroot creation script
- The update script to setup development
- 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:
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
../deadite/ to match your paths.
Next get in the chroot by running the command:
Within the chroot environment, you’ll need to run the
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.