Blog » Compiling Qt5 for Raspberry Pi
Compiling Qt5 for Raspberry Pi
Qt is great easy to use multiplatform C++ framework. Raspberry Pi is a great piece of widely used hardware. In this blog post I'd like to show how to mix these technologies together - compile Qt5 from sources for Raspberry Pi. In one of following posts I'll also show how to configure QtCreator for remote debug on Raspberry Pi.
The toolchain for cross-compiling
The idea is to use standard Linux based PC with QtCreator for development. For this purpose we need cross-compile toolchain which is able to run on x64 or .i686 platform and compile for ARMv6 platform. Such toolchain (GCC based) can be build from sources using crosstool-ng. But it's possible and easier to use pre-build toolchain that is available.
Note: Following tutorial was created with clean install of Fedora 25 x64 running in VirtualBox. You can use your favourite distro for it also.
Lets start by downloading a toolchain. We're going to checkout git repository from github.
git clone https://github.com/raspberrypi/tools.git
I'd like to place my toolchain into /opt/raspi_toolchain directory. So let's create it and set rights
sudo mkdir /opt/raspi_toolchain
sudo chmod 0777 /opt/raspi_toolchain
Now copy cloned prebuild toolchain to that directory
cp -a tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/* /opt/raspi_toolchain
Raspberry Pi root file system
We also need target root file system for build. Root file system contains all include headers and libraries. It is simply the content of ext4 partition of your Raspberry Pi micro SD card. To be able to compile our Qt applications without micro SD card mounted to your development system we will copy the card content to local directory. I will choose /opt/raspi_rootfs. We mount the file system (ext4 on micro SD card is /dev/sdb2 in my case) and copy it to /opt/raspi_rootfs directory.
Note: I'm using archlinuxarm for Raspberry Pi. It's lightweight and fast booting. Please follow installation tutorial on https://archlinuxarm.org/platforms/armv6/raspberry-pi.
sudo mount /dev/sdb2 /tmp/rootfs
sudo mkdir /opt/raspi_rootfs
sudo chmod 0777 /opt/raspi_rootfs
sudo cp -a /tmp/rootfs/* /opt/raspi_rootfs
Now we have everything ready for Qt5 build.
Let's download and untar Qt source code
tar -xvf qt-everywhere-opensource-src-5.7.0.tar.gz
Now important step - configuring Qt build. This is little bit tricky. I've found following config to be OK. It will build and place qmake to /opt/qt5pi and all libraries and includes to /opt/raspi_rootfs/opt/qt5pi. It is important to finish configuration without any error.
./configure -opengl es2 -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=/opt/raspi_toolchain/bin/arm-linux-gnueabihf- -opensource -confirm-license -optimized-qmake -reduced-exports -nomake examples -nomake tests -make libs -sysroot /opt/raspi_rootfs -hostprefix /opt/qt5pi -prefix /opt/qt5pi
Now let's build, have a very long coffee break and then install.
sudo gmake install
We have Qt5 ready in our local root file system when everything passed. We have to copy it to micro SD card to be able to run Qt applications on Raspberry Pi
sudo cp -an /opt/raspi_rootfs/* /tmp/rootfs
sudo umount /dev/sdb2
Following steps are needed to cross-compile and debug using QtCreator for Raspberry Pi. First we have to specify a new device. Go to Toos -> Options in main menu and click Devices. Click Add button, select "Generic Linux Device" and click Start Wizard button. In following dialog fill the name, IP address and authentication info (I assume IP address of Raspberry Pi to be 192.168.1.199). It should look like this (click Apply after finish):
Now continue by adding a compiler (our cross-compile toolchain in /opt/raspi_toolchain). Click Build & Run and select Compilers tab. Click Add and select GCC. Specify the name and path to the compiler (click Apply after finish):
Let's continue by adding new Qt version (the one we've build). Select Qt Versions tab and click Add button. File Open dialog will appear - find qmake that should be in /opt/qt5pi/bin directory and click OK. You can also change the name of the compiler (click Apply after finish):
And finally add a new kit. Select Kits tab and click Add button. Set the name, select the device type as Generic Linux Device and select device to one we've created. Compiler should be set to our Raspberry Pi GCC compiler and Qt version to our Qt version we've created. It should look like this (click Apply after finish):
Click OK. Everything is set now and ready to use.
QtCreator Example project
Create new project by clicking File -> New File or Project. Select Application, Qt Console Application and click Choose button. Fill project name and destination directory and click Next button. In following dialog check Raspberry Pi kit and uncheck Desktop (you can have both kits selected and switch them before build). Click Next and Finish.
Now new project is ready. Let's create new class Task with some stdout output.
class Task : public QObject
explicit Task(QObject *parent = 0);
#endif // TASK_H
using namespace std;
Task::Task(QObject *parent) : QObject(parent)
cout << "Hello world from Qt" << endl;
int main(int argc, char *argv)
QCoreApplication a(argc, argv);
Task *task = new Task(&a);
QTimer::singleShot(0, task, SLOT(run()));
Now click Build -> Build all and executable binary should build. Now we have to copy built binary to micro SD card, put it to Raspberry Pi, boot, log in and execute. You should be able to see "Hello world from Qt" text after executing it.
[root@alarmpi ~]# ./test
Hello world from Qt