Cross compile and remote debug Qt applications on Raspberry Pi
Mon, Nov 23, 2020
Cross compile and remote debug Qt applications on Raspberry Pi. These instructions allow you to build Qt applications that will run and display graphics without X running on the Pi. This is useful for embedded configurations.
sudo raspi-config
sudo nano /etc/apt/sources.list # uncomment the deb-src line
sudo apt-get update
sudo apt-get upgrade
sudo apt-get build-dep qt4-x11
sudo apt-get build-dep qtbase-opensource-src
sudo apt-get install libudev-dev libinput-dev libts-dev libxcb-xinerama0-dev libxcb-xinerama0 libgl1-mesa-dri libts-0.0-0
# Create destination folder
sudo mkdir /usr/local/qt5pi
sudo chown pi /usr/local/qt5pi
# this version will require env LD_LIBRARY_PATH=/opt/vc/lib when running
sudo ln -s libbrcmEGL.so /opt/vc/lib/libEGL.so.1
sudo ln -s libbrcmGLESv2.so /opt/vc/lib/libGLESv2.so.2
# Fix that doesn't require LD_LIBRARY_PATH
sudo mv /usr/lib/arm-linux-gnueabihf/libEGL.so.1.1.0 /usr/lib/arm-linux-gnueabihf/libEGL.so.1.1.0_backup
sudo mv /usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.1.0 /usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.1.0_backup
sudo ln -s /opt/vc/lib/libbrcmEGL.so /usr/lib/arm-linux-gnueabihf/libEGL.so.1.1.0
sudo ln -s /opt/vc/lib/libbrcmGLESv2.so /usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.1.0
export PIDEV=<your-pi-ip-addr>
mkdir ~/raspi
cd ~/raspi
git clone https://github.com/raspberrypi/tools
# Create sysroot
mkdir sysroot sysroot/usr sysroot/opt
rsync -avz pi@$PIDEV:/lib sysroot
rsync -avz pi@$PIDEV:/usr/include sysroot/usr
rsync -avz pi@$PIDEV:/usr/lib sysroot/usr
rsync -avz pi@$PIDEV:/opt/vc sysroot/opt
# Update relative links
wget https://raw.githubusercontent.com/Kukkimonsuta/rpi-buildqt/master/scripts/utils/sysroot-relativelinks.py
chmod +x sysroot-relativelinks.py
./sysroot-relativelinks.py sysroot
# Download Qt source
git clone git://code.qt.io/qt/qtbase.git -b 5.13.1
git clone git://code.qt.io/qt/qtdeclarative.git -b 5.13.1
git clone git://code.qt.io/qt/qtquickcontrols2.git -b 5.13.1
git clone git://code.qt.io/qt/qtsvg.git -b 5.13.1
git clone git://code.qt.io/qt/qtvirtualkeyboard.git -b 5.13.1
# Build Qt
cd qtbase
./configure -release -opengl es2 -no-icu -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- -sysroot ~/raspi/sysroot -opensource -confirm-license -make libs -prefix /usr/local/qt5pi -extprefix ~/raspi/qt5pi -hostprefix ~/raspi/qt5 -v
make -j8
make install -j8
cd ..
cd qtdeclarative
~/raspi/qt5/bin/qmake
make -j8
make install -j8
cd ../qtquickcontrols2
~/raspi/qt5/bin/qmake
make -j8
make install -j8
cd ../qtsvg
~/raspi/qt5/bin/qmake
make -j8
make install -j8
cd ../qtvirtualkeyboard
~/raspi/qt5/bin/qmake
make -j8
make install -j8
cd ..
# If something goes wrong, you can reset the git folder with "git clean -dfx"
# copy qt to rpi
rsync -avz qt5pi pi@$PIDEV:/usr/local
# build example
cd qtbase/examples/opengl/qopenglwidget
~/raspi/qt5/bin/qmake
make
scp qopenglwidget pi@$PIDEV:/home/pi
env LD_LIBRARY_PATH=/usr/local/qt5pi/lib:/opt/vc/lib ./qopenglwidget
If you are going to be doing a lot of development and this is the only Qt application you’re interested in you can add the new library path.
echo /usr/local/qt5pi/lib | sudo tee /etc/ld.so.conf.d/00-qt5pi.conf
sudo ldconfig
./qopenglwidget
qt.qpa.screen: QXcbConnection: Could not connect to display
Could not connect to any X display.
You’re probably using the system qt libraries. Test with
$ ldd ./qopenglwidget | grep 'libQt'
libQt5Widgets.so.5 => /usr/lib/arm-linux-gnueabihf/libQt5Widgets.so.5 (0x769bb000)
libQt5Gui.so.5 => /usr/lib/arm-linux-gnueabihf/libQt5Gui.so.5 (0x7653b000)
libQt5Core.so.5 => /usr/lib/arm-linux-gnueabihf/libQt5Core.so.5 (0x760c1000)
Make sure you have updated ldconfig or use the env LD_LIBRARY_PATH=/usr/local/qt5pi/lib
approach.
If you are linking with the right libraries it should look like this:
$ ldd ./qopenglwidget | grep 'libQt'
libQt5Widgets.so.5 => /usr/local/qt5pi/lib/libQt5Widgets.so.5 (0x76a25000)
libQt5Gui.so.5 => /usr/local/qt5pi/lib/libQt5Gui.so.5 (0x765aa000)
libQt5Core.so.5 => /usr/local/qt5pi/lib/libQt5Core.so.5 (0x7608f000)
This means the EGL library could not be loaded. Make sure you did Fix OpenGL libraries step above.
$ ./qopenglwidget
Could not initialize egl display
Aborted
ldd /usr/local/qt5pi/plugins/platforms/libqeglfs.so | grep GL
Fix OpenGL libraries
above