Skip to content

Instantly share code, notes, and snippets.

@matt2005
Forked from kelixlabs/qtrpi-cross-compile.md
Created December 8, 2021 20:32
Show Gist options
  • Save matt2005/d60f94e38d948d48357866c01a3e1be0 to your computer and use it in GitHub Desktop.
Save matt2005/d60f94e38d948d48357866c01a3e1be0 to your computer and use it in GitHub Desktop.
Cross-compiling Qt for HW accelerated OpenGL with eglfs on Raspbian and setting up Qt Creator

QT-Raspi Cross Compile

Prepare the Raspberry Pi:

Install raspbian

Get the latest raspbian image here

Enable deb-src repos

Uncomment the deb-src line in /etc/apt/sources.list and update raspbian

sudo nano /etc/apt/sources.list
sudo apt-get update
sudo apt-get upgrade
sudo shutdown -r now

Update Raspberry Pi firmware

sudo rpi-update
sudo shutdown -r now

Run raspi-config

change the GPU memory to 256MB and boot with "wait for network on boot"

sudo raspi-config

(Optional) Install and configure touch-screen display.

Install dependencies

sudo apt-get build-dep qt4-x11
sudo apt-get build-dep libqt5gui5
sudo apt-get install libudev-dev libinput-dev libts-dev libxcb-xinerama0-dev libxcb-xinerama0
sudo apt-get install cmake

Optional

Bluetooth
sudo apt-get install bluez libbluetooth-dev
Audio & gstreamer multimedia
sudo apt-get install libasound2-dev pulseaudio libpulse-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-plugins-bad libgstreamer-plugins-bad1.0-dev gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-alsa

Printing support

sudo apt-get install libcups2-dev

Accessibility

sudo apt-get install libatspi-dev

Install mysql c connector from source

Download mysql c connector source from here (Source code, Generic Linux (Arsitecture Independent))

i will install it on /opt/local/mysql-rpi-src

tar zxvf mysql-connector-c-6.1.11-src.tar.gz
cd mysql-connector-c-6.1.11-src
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/opt/local/mysql-rpi-src
make
sudo make install

Also add missing symbolic links

sudo ln -s /opt/local/mysql-rpi-src/lib/libmysqlclient.so.18 /usr/lib/arm-linux-gnueabihf/

Prepare our target directory

sudo mkdir /usr/local/qt5pi
sudo chown pi:pi /usr/local/qt5pi

Prepare Linux host:

I use Ubuntu x64 and Qt 5.10.1

Update host

sudo apt-get update
sudo apt-get -y upgrade

Install dependencies

sudo apt-get install git bison python gperf

Generate ssh public key

ssh-keygen -t rsa
ssh-copy-id pi@<raspi ip>

change <raspi ip> address of Raspberry Pi

Create Working directory

sudo mkdir /opt/raspi
sudo chmod a+w /opt/raspi
cd /opt/raspi

Get a toolchain

git clone https://github.com/raspberrypi/tools

Add toolchain binary directory to PATH. Open .bashrc

nano ~/.bashrc

and add line at end of file

export PATH=$PATH:/opt/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin

update bashrc

source ~/.bashrc

Create a sysroot

Using rsync we can properly keep things synchronized in the future as well

mkdir -p sysroot/{opt,usr}
rsync -avz pi@<raspi ip>:/lib sysroot
rsync -avz pi@<raspi ip>:/usr/include sysroot/usr
rsync -avz pi@<raspi ip>:/usr/lib sysroot/usr
rsync -avz pi@<raspi ip>:/opt/vc sysroot/opt
rsync -avz pi@<raspi ip>:/opt/local sysroot/opt/

Also copy mysql-rpi-src to host /opt This use for ./configure flag: MYSQL_PREFIX=/opt/local/mysql-rpi-src

rsync -avz pi@<raspi ip>:/opt/local /opt/

change <raspi ip> address of Raspberry Pi

Fix the EGL/GLES libraries

Create correct symlinks to "missing" libraries

mv sysroot/usr/lib/arm-linux-gnueabihf/libEGL.so.1.0.0 sysroot/usr/lib/arm-linux-gnueabihf/libEGL.so.1.0.0_backup
mv sysroot/usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.0.0 sysroot/usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.0.0_backup
ln -s sysroot/opt/vc/lib/libEGL.so sysroot/usr/lib/arm-linux-gnueabihf/libEGL.so.1.0.0
ln -s sysroot/opt/vc/lib/libGLESv2.so sysroot/usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.0.0

Please make sure to also add missing symbolic links

ln -s sysroot/opt/vc/lib/libEGL.so sysroot/opt/vc/lib/libEGL.so.1
ln -s sysroot/opt/vc/lib/libGLESv2.so sysroot/opt/vc/lib/libGLESv2.so.2

Adjust symlinks to be relative

Use provided script, because the old fixQualifiedLibraryPaths is not working properly

wget https://raw.githubusercontent.com/riscv/riscv-poky/priv-1.10/scripts/sysroot-relativelinks.py
chmod +x sysroot-relativelinks.py
./sysroot-relativelinks.py sysroot

Configure Qt Source

use Qt source

wget https://download.qt.io/official_releases/qt/5.10/5.10.1/single/qt-everywhere-src-5.10.1.tar.xz

and extract it

tar Jxvf qt-everywhere-src-5.10.1.tar.xz

Create hf configuration

cp -rv qt-everywhere-src-5.10.1/qtbase/mkspecs/linux-arm-gnueabi-g++ qt-everywhere-src-5.10.1/qtbase/mkspecs/linux-arm-gnueabihf-g++
sed -i -e 's/arm-linux-gnueabi-/arm-linux-gnueabihf-/g' qt-everywhere-src-5.10.1/qtbase/mkspecs/linux-arm-gnueabihf-g++/qmake.conf

Create build dir

mkdir qt5build
cd qt5build

Configure Qt

../qt-everywhere-src-5.10.1/configure -opengl es2 -device linux-rasp-pi3-g++ -device-option CROSS_COMPILE=arm-linux-gnueabihf- -sysroot /opt/raspi/sysroot -prefix /usr/local/qt5pi -opensource -confirm-license -plugin-sql-mysql MYSQL_PREFIX=/opt/local/mysql-rpi-src -recheck-all -skip qtwebengine -skip qtscript -make libs -no-use-gold-linker -v

If you failed, you can delete qt5build directory and try again

cd ..
rm -rvf qt5build

use qtbase

git clone git://code.qt.io/qt/qtbase.git -b <qt-version>
cd qtbase

change <qt-version> with a proper Qt version I use 5.10; Note that version 5.10.1 is a tag not a branch, so you may want to create a local branch with it

Configure Qt

./configure -opengl es2 -device linux-rasp-pi3-g++ -device-option CROSS_COMPILE=arm-linux-gnueabihf- -sysroot /opt/raspi/sysroot -prefix /usr/local/qt5pi -opensource -confirm-license -plugin-sql-mysql MYSQL_PREFIX=/opt/local/mysql-rpi-src -recheck-all -skip qtwebengine -skip qtscript -make libs -no-use-gold-linker -v

If you failed, you can clear everything with:

git clean -dfx
Notes

Sadly QtWebengine dont compile on Raspberry Pi 1 (maybe also Pi 2), so we have to skip it. Also skip QtScript, its deprecated.

For every version of Raspberry Pi, you have to change -device 
Raspberry Pi 1 (+ Zero and Zero W): -device linux-rasp-pi-g++
Raspberry Pi 2: -device linux-rasp-pi2-g++
Raspberry Pi 3: -device linux-rasp-pi3-g++
Raspberry Pi 3 with VC4 driver: -device linux-rasp-pi3-vc4-g++

Make and Install Qt

You probably also want to add -jn option to make command, where n is a number of cores you like to use for the complication

make
make install

Deploy Qt to the device

We simply sync everything from /opt/raspi/sysroot/usr/local/qt5pi to the prefix we configured above

cd /opt/raspi
rsync -avz sysroot/usr/local/qt5pi pi@<raspi ip>:/usr/local

change <raspi ip> address of Raspberry Pi

Qt configuration

Once Qt is on the device, Qt Creator can be set up to build, deploy, run and debug Qt apps directly on the device with one click.

Go to Options -> Devices
  Add
    Change the name
    Generic Linux Device
    Enter IP address
    authentication type key
    user & add private key ( ~/.ssh/id_rsa )
    Finish
Go to Options -> Build&Run -> Debuggers
  Add
    /opt/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gdb
Go to Options -> Build & Run -> Compilers
  Add
    GCC -> C++
    Compiler path: /opt/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++

    ABI: arm - linux - generic - elf - 32bit
  Add
    GCC -> C
    Compiler path: /opt/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc

    ABI: arm - linux - generic - elf - 32bit
Go to Options -> Build & Run -> Qt Versions
  Add
    /opt/raspi/sysroot/usr/local/qt5pi/bin/qmake
Go to Options -> Build & Run -> Kits
  Add
    Change the name & icon
    Device type: Generic Linux Device
    Device: the one we just created
    Sysroot: ~/raspi/sysroot
    Compiler: the one we just created
    Debugger: the one we just created
    Qt version: the one we saw under Qt Versions
    Qt mkspec: leave empty

Done. At this point you should be able to start a new project with the new kit, build and deploy it, etc.

If you press compile Button inside QtCreator, source will just compile. If you press the green Play button, source will compile, upload binary with sftp and executed on your Raspberry Pi. Debug button should also work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment