Introduction
This is a guide for cross-compiling Qt 5.15.2 for Raspberry Pi 4 and installing it on the Compute Module 4. It's an update to my blog post Qt on the Raspberry Pi 4, with the difference that this time I'm using Raspberry Pi OS Lite (download here: https://www.raspberrypi.org/software/operating-systems/), Qt version 5.15.2 and Ubuntu 20 LTS as a cross-compile computer in a virtual machine.
Sources
In addition to my old blog post (see above), I also used the following sources:
- by Uvindu Wijesinghe: https://github.com/UvinduW/Cross-Compiling-Qt-for-Raspberry-Pi-4
Attention
There is an update to this article, which contains cross compilation for Raspberry Pi, Qt6 and Ubuntu 22.04 LTS. Follow this link, if you need newer versions.
Raspberry Pi OS Lite
Install Raspberry Pi OS Lite on a Raspberry Pi 4 or on a Raspberry Compute Module 4 as described in my blog post Installing Raspberry Pi OS on Raspberry Compute Module 4 .
Qt 5.15.2 on Ubuntu 20 LTS
After the Raspberry Pi OS is installed on the Compute Module and the Raspberry boots back from the eMMC memory, it's time to install the required software on the Raspberry and on the Ubuntu machine.
Raspberry Compute Module 4
The steps shown below should also work on a "normal" Raspberry Pi 4.
After switching on the Pi 4, open the configuration menu.
sudo raspi-config
For our configuration we need "SSH" and "GL (Fake KMS)". See the following two screenshots.- Then add development sources to /etc/apt/sources.list. To do this, add the following line:
deb-src http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi
- Then update the system with the following commands:sudo apt-get update
sudo apt-get dist-upgrade
sudo reboot
sudo rpi-update
sudo reboot
- And then install the required Qt and development packages:sudo apt-get build-dep qt5-qmake
sudo apt-get build-dep libqt5gui5
sudo apt-get build-dep libqt5webengine-data
sudo apt-get build-dep libqt5webkit5
sudo apt-get install libudev-dev libinput-dev libts-dev libxcb-xinerama0-dev libxcb-xinerama0 gdbserver
- Then create a directory for RaspberryQt:sudo mkdir /usr/local/qt5.15
sudo chown -R pi:pi /usr/local/qt5.15
Ubuntu Machine
A PC or virtual machine with Ubuntu 20 LTS installed is required.
First of all, bring Ubuntu up to date and install some required libraries:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install gcc git bison python gperf pkg-config gdb-multiarch
sudo apt install build-essential
Then - if not already available - create an ssh-key and install it on the Raspberry, so that a password prompt does not appear with every rsync and username and password have to be entered. There are enough instructions on the Internet, so I will spare myself the detailed description here.
Create directory structure for the Raspberry libraries
For the required files, I create the following directory structure under Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4:
sudo mkdir ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4
sudo mkdir ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/build
sudo mkdir ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/tools
sudo mkdir ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot
sudo mkdir ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/usr
sudo mkdir ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/opt
sudo chown -R 1000:1000 ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4
cd ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4
Download Qt Resources
We download the Qt resources and unpack them in the raspberrypi4 directory:
sudo wget http://download.qt.io/archive/qt/5.15/5.15.2/single/qt-everywhere-src-5.15.2.tar.xz
sudo tar xfv qt-everywhere-src-5.15.2.tar.xz
Now we need to modify the mkspec file a bit so that we can use it with our compiler. To do this, run the following commands:
cp -R qt-everywhere-src-5.15.2/qtbase/mkspecs/linux-arm-gnueabi-g++ qt-everywhere-src-5.15.2/qtbase/mkspecs/linux-arm-gnueabihf-g++
sed -i -e 's/arm-linux-gnueabi-/arm-linux-gnueabihf-/g' qt-everywhere-src-5.15.2/qtbase/mkspecs/linux-arm-gnueabihf-g++/qmake.conf
Download Cross-Compiler
As a cross-compiler I use a Linaro version 7.4.1. To do this, change to the tools directory and download and unpack the compiler:
cd tools
sudo wget https://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/arm-linux-gnueabihf/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz
tar xfv gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz
Rsync of Raspberry Libraries
Now we need the original libraries from the Raspberry Pi, which we copy into the Ubuntu directories with rsync:
cd ..
rsync -avz --rsync-path="sudo rsync" --delete [email protected]:/lib sysroot/
rsync -avz --rsync-path="sudo rsync" --delete [email protected]:/usr/include sysroot/usr/
rsync -avz --rsync-path="sudo rsync" --delete [email protected]:/usr/lib sysroot/usr/
rsync -avz --rsync-path="sudo rsync" --delete [email protected]:/opt/vc sysroot/opt/
Now we have to clean up the symbolic links that were copied by the rsync so that they point to the correct original files. There is a small Python script for download:
wget https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py
Then make the script executable and call it:
sudo chmod +x sysroot-relativelinks.py
./sysroot-relativelinks.py sysroot
Compiling Qt
Now we can configure the build and then compile it.
cd build
../qt-everywhere-src-5.15.2/configure -release -opengl es2 -eglfs -device linux-rasp-pi4-v3d-g++ -device-option CROSS_COMPILE=~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/tools/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf- -sysroot ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot -prefix /usr/local/qt5.15 -extprefix ~/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/qt5.15 -opensource -confirm-license -skip qtscript -skip qtwayland -skip qtwebengine -nomake tests -make libs -pkg-config -no-use-gold-linker -v -recheck
After a few minutes, the script should be finished and the following EGLFS conditions should be set or not set.
QPA backends:
DirectFB ............................... no
EGLFS .................................. yes [SHOULD BE YES]
EGLFS details:
EGLFS OpenWFD ........................ no
EGLFS i.Mx6 .......................... no
EGLFS i.Mx6 Wayland .................. no
EGLFS RCAR ........................... no
EGLFS EGLDevice ...................... yes [SHOULD BE YES]
EGLFS GBM ............................ yes
EGLFS VSP2 ........................... no
EGLFS Mali ........................... no
EGLFS Raspberry Pi ................... no [SHOULD BE NO]
EGLFS X11 ............................ yes
LinuxFB ................................ yes
VNC .................................... yes
If this is not the case or if any other error messages appear, please investigate and clean up. If you want to run the configuration script again with changed variables, please delete the contents of the build directory beforehand.
If everything is OK, then run the make and make install commands.
make -j4
make install
Install compiled files on the Raspberry
If the compilation is successful, the compiled files - they are located in the qt5.15 directory - can be copied to the Raspberry Pi. Again, we do this using the rsync command.
rsync -avz --rsync-path="sudo rsync" qt5.15 [email protected]:/usr/local/
Configuration QtCreator
In the next blog post I will explain how to use QtCreator for use with the compiled libraries.