Qt on Raspberry Pi 3 Model B+

At Ambes, we like to research, evaluate and experiment (REX) all the wonders of the embedded world. Our REX approach offers us new insights into problem-solving of our daily project issues.

In this series of tutorials, I would like to guide you through building and using Qt Framework on several popular single board computers.

This article covers the steps of preparing and building Qt Framework for Raspberry Pi 3 Model B+. We will then use it to cross-compile and deploy our applications.

This tutorial is intended for usage with GNU/Linux distributions.

Getting ready

If you already have the latest Raspbian image, skip to section Preparing environment

To start, first download the latest Raspbian image.

You can find it on
https://www.raspberrypi.org/downloads/raspbian/

Or directly on

https://downloads.raspberrypi.org/raspbian_latest

After the download is complete, burn the image and update Raspbian.
To do this, boot Raspberry Pi with the burnt SD card. When you are logged in, open terminal and enter commands:

sudo apt-get update
sudo apt-get dist-upgrade

Now power off Raspberry Pi and insert the SD Card into your card reader.

Preparing environment

There are many different ways to prepare the environment for the development. I will show you how to do it by using a copy of the SD Card image.

Open a terminal application in your GNU/Linux environment; it will be your closest friend until the tutorial completion.

Create an image of the SD Card. You can do this by using dd command.

sudo
dd if=/dev/sdX2 of=raspbian_updated_image.img

where X is a letter (a,b,c,…). /dev/sdX2 is the path to SD Card device (e.g. /dev/sdc2). You can check this with command dmesg that shows you the label of the inserted storage device.



Be very careful while executing dd command! If used incorrectly, it can make irreparable damage to your data.

Create a mount point for the SD Card:

mkdir /mnt/raspbian-qt

Now mount the image to this new created directory:

sudo mount raspbian_update_image.img -o loop /mnt/raspbian-qt

Create a directory for building qt and downloading all the tools.

I will use directory named rpi_qt

 

cd ~

mkdir rpi_qt

cd rpi_qt

 

To build Qt you either need to download Raspberry Pi toolchain or build your own. Building your own toolchain will be covered in a later article.

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

Download latest QtBase code:

git clone git://code.qt.io/qt/qtbase.git

Check if everything is downloaded with command ls.

It should look something like this:

CONFIGURE QT

Enter qtbase directory

cd qtbase

and select Qt version (or leave at latest default).

To check the currently selected version, use

git status

Using git checkout x.xx.x you can choose a specific version.

git checkout 5.11.1

First step is to build qmake, which is the basis for building all the other modules. This is a crucial step as it defines location of your host libraries and sysroot.

 

Sysroot is short for system root, a directory where Qt will look for Raspberry Pi’s libraries and headers.

 

In our case, it’s located at /mnt/raspbian-qt.

So, before the building step, we first define parameters of the build.

For this, we use configure script, found in QtBase.

./configure -opengl es2 -device linux-rasp-pi3-g++ -device-option CROSS_COMPILE=~/rpi_qt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- -sysroot /mnt/raspbian-qt -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -prefix /usr/local/qt5pi -hostprefix /usr/local/qt5pi

Breaking down the parameters:

  • -opengl es2
    • We will use OpenGLES2 acceleration
  • -device linux-rasp-pi3-g++
    • Device selection
    • You can check other available devices and change parameters in mkspecs/devices
  • -device-option CROSS_COMPILE=~/rpi_qt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-
    • Setting cross compile tools path
  • -sysroot /mnt/raspbian-qt
    • Sysroot path
  • -opensource
    • Selecting Qt Open source license
  • -confirm-license
    • Confirm license choice, otherwise it asks you to confirm during configuration
  • -optimized-qmake -reduce-exports -release -make libs
    • Use qmake optimizations


Important! Use Absolute paths!

If everything goes as planned, you should see a message like this at the end of the configuration.

Now scroll up and check if you’ve configured Qt for using OpenGL.

If you find yes, everything is set and ready for building the modules.

BUILDING QT MODULES

QtBase is configured and ready to build.

To build a module, run make command with parameter -j which defines how many threads you will use. Easiest way to determine it is to use as many threads as there are processor cores in your PC.

e.g. for Intel i7 8 cores issue:

make -j8

If no errors appear, build is successfully completed. Congratulations!

Now install the built components.

sudo make install

Check if module is installed:

qmake is located in bin directory.

This is important for later configuration of other Qt modules.

Clone, configure, build and install

Let’s build QtQuickControls2.

Find the module and it’s requirements and dependencies on Qt official Git repository:

https://github.com/qt/

or

https://code.qt.io/cgit/qt

 The dependencies for QtQuickControls2 are qtbase, qtxmlpatterns and qtdeclarative.

In following example we will build qtxmlpatterns.

For each module you wish to build go through following steps:

  • Find the desired module and clone the git repository
    • Find the module on https://code.qt.io/cgit/
    • Clone the module
      • e.g. git clone git://code.qt.io/qt/qtxmlpatterns.git
    • Enter directory of the module
      • cd qtxmlpatterns
  • Configure the module with the built qmake
    • /usr/local/qt5pi/bin/qmake .
  • Build the module
    • make -jX
    • where X is number of used threads for building
  • Install the module
    • sudo make install

Go back a level (cd ..) and use the above steps to build the rest of the needed modules.

After building all the modules, proceed to the next section.

SETTING UP QTCREATOR

To deploy your application on Raspberry Pi, we first need to setup QtCreator.

We will go through following steps:

  • Add Raspberry Pi as device for application deployment
  • Add Raspberry Pi C and C++ compilers
  • Add built qmake
  • Create a Qt kit setting

Open QtCreator and go to Tools → Options…DevicesDevices Tab.

Click button Add… and input all the needed info about your Raspberry Pi system.

After accepting, your Devices tab should look something like this:

Go to Kits in the same window. Open tab Compilers. Click Add button and select GCC → C.

Find your gcc compiler:

e.g. ~/rpi_qt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/bin/gcc

Do the same for GCC → C++

e.g. ~/rpi_qt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/bin/g++

Go to Qt Versions tab.

Click Add… button and find qmake you’ve built.

e.g. /usr/local/qt5pi/bin/qmake

If there are no error/warning signs on the side of any of set parameters, we are good to go!

Go to Kits tab.

Click Add and set all the parameters.

No errors? Congratulations! You have succeeded in setting up your Qt Creator for remote Raspberry Pi deployment.

Now open your desired project, configure it using your Raspberry Pi Kit and press Run to fully appreciate the hard work put into these cross compiling preparations.

No Comments

Post a Comment

Comment
Name
Email
Website