Qt5 on RaspberryPi

From Qt Wiki
Revision as of 16:37, 3 March 2015 by AutoSpider (talk | contribs) (Add "cleanup" tag)
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

h1. Simplified guide for getting Qt 5 built and installed on the raspberry pi

Overview

Since the instructions at the RaspberryPi wiki-page can be a bit confusing I've created this step-by-step guide to document what works for me. I'm using Ubuntu 12.04, but I don't see why the instructions wouldn't work on older / other distros.

Update: I was made aware that another similar guide already existed, and it looks to be more complete than this guide, please see RaspberryPi_Beginners_guide

Preparing the sysroot and getting the toolchain

Download the Debian Wheezy image from here and flash it on your device: "raspberrypi.org download page":http://www.raspberrypi.org/downloads

The instructions here are tested with the 2012-08-16-wheezy-raspbian image, but should work with 2012-07-15-wheezy-raspbian as well.

Replace /dev/sde with the location of your SD card as reported by "sudo fdisk -l". Note that this will overwrite any previous contents on the SD card.

unzip 2012-08-16-wheezy-raspbian.zip
sudo dd if=2012-08-16-wheezy-raspbian.img of=/dev/sde bs=1M
sudo eject /dev/sde

We also mount the wheezy image at /mnt/rasp-pi-rootfs for use as a sysroot during cross compilation of Qt 5. The offset 62914560 is computed by running "sudo fdisk -l" on the .img file, and multiplying the sector size 512 bytes with the start of the Linux partition at sector 122880. It's the same for 2012-07-15-wheezy-raspbian. Remember to re-mount the image in case you reboot as well, otherwise a lot of confusion might ensue.

sudo mount -o loop,offset=62914560 2012-08-16-wheezy-raspbian.img /mnt/rasp-pi-rootfs

Get and extract the Linaro toolchain graciously built and provided by Donald: "gcc-4.7-linaro":http://blueocean.qmh-project.org/gcc-4.7-linaro-rpi-gnueabihf.tbz

mkdir ~/rpi
cd~/rpi
wget http://blueocean.qmh-project.org/gcc-4.7-linaro-rpi-gnueabihf.tbz
tar -xjf gcc-4.7-linaro-rpi-gnueabihf.tbz

The main site seem's down, there is a mirror on http://swap.tsmt.eu/gcc-4.7-linaro-rpi-gnueabihf.tbz

Get and run the fixQualifiedLibraryPaths script from cross-compile-tools in order to fix broken symlinks in the image.

cd ~/rpi
git clone https://git.gitorious.org/cross-compile-tools/cross-compile-tools.git
cross-compile-tools/fixQualifiedLibraryPaths /mnt/rasp-pi-rootfs~/rpi/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc

Preparing the Raspberry Pi

The first time you boot the Raspberry Pi you get a configuration menu. Otherwise, manually run raspi-config. Make sure a keyboard is connected via USB, and that the raspberry pi is connected to your local area network via a network cable. From this menu you'll want to run the expand_rootfs option, enable ssh server, set the boot_behaviour to no to prevent X from starting (since we'll be running Qt directly on top of fullscreen EGL). Also in the memory_split option give VideoCore 128 MB of memory to be able to run the most GPU hungry QML 2 applications.

Also install rsync which we will later use to copy over the Qt libraries and binaries, and set the root password to enable rsync'ing to the root user.

# on the raspberry pi, as user pi (password raspberry)
sudo apt-get update
sudo apt-get install rsync
sudo passwd
# enter root password, I chose raspberry here, same as for the pi user
sudo reboot

After the raspberry has rebooted it will tell you its IP address above the login prompt. Keep track of this IP address for later.

Building qtbase, qtjsbackend, and qtdeclarative

First we clone the Qt source packages we're interested in to be able to run qmlscene and QML 2 applications. qtjsbackend needs this not yet integrated patch to work on armv6, so we cherry-pick it: https://codereview.qt.io/#change,27256

cd ~/rpi
git clone git://gitorious.org/qt/qtbase.git
git clone git://gitorious.org/qt/qtjsbackend.git
(cd qtjsbackend && git fetch https://codereview.qt.io/p/qt/qtjsbackend refs/changes/56/27256/4 && git cherry-pick FETCH_HEAD)
git clone git://gitorious.org/qt/qtdeclarative.git

Next we configure and build qtbase. I've chosen the prefix /opt/qt5, which is where the qt libraries etc will end up on the device image. You might need to sync qtbase back to change 069469b468c482244c5, since I experienced build issues with the latest HEAD. If you get any build failures it's a good idea to do "git clean -dxf" in qtbase before trying again, to remove any by-products of the failed build. Note that this will erase any local changes you've made to qtbase, so use with caution.

cd ~/rpi/qtbase
git checkout 069469b468c482244c5 # if HEAD doesn't build
./configure -prefix /opt/qt5 -release -device linux-rasp-pi-g++ -make libs -device-option CROSS_COMPILE=~/rpi/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf- -sysroot /mnt/rasp-pi-rootfs
# accept the license agreement
make
sudo make install

We're ready to build and install the other modules now, using the qmake built together with qtbase. Since it's a host tool it's installed in /opt/qt5/bin outside of the sysroot at /mnt/rasp-pi-rootfs.

Due to a build error I encountered with the latest qtdeclarative I had to use "sudo make". For reference, the SHA-1 of qtjsbackend I tested was e49f760da4b42c96e9f (+ the cherry-pick above), and qtdeclarative was at bf5f97ae626af46742c315.

cd~/rpi/qtjsbackend
/opt/qt5/bin/qmake
make
sudo make install
cd ~/rpi/qtdeclarative
/opt/qt5/bin/qmake
sudo make
sudo make install

Since qmlscene doesn't get built automatically, we do it manually. We also build samegame to have a QML 2 example to test.

cd~/rpi/qtdeclarative/tools/qmlscene
/opt/qt5/bin/qmake
make
sudo make install
cd ~/rpi/qtdeclarative/examples/demos/samegame/
/opt/qt5/bin/qmake
make
sudo make install

Getting our /opt/qt5 onto the device

Now it's just a matter of rsync'ing our /opt/qt5 to the device. Exchange the IP address with your own raspberry pi's IP address as noted earlier (displayed right after the pi's boot, above the login prompt). Note the trailing slash on the source directory, without it you'll end up with /opt/qt5/qt5 on the device.

rsync -av /mnt/rasp-pi-rootfs/opt/qt5/ root@172.24.91.127:/opt/qt5/

Trying out stuff

ssh into the device as the pi user, and run an example.

# on the host
ssh pi@172.24.91.127
# on the pi
export LD_LIBRARY_PATH=/opt/qt5/lib/
# to prevent QML 2 animations from running too slow
export QML_FIXED_ANIMATION_STEP=no
/opt/qt5/examples/qtdeclarative/demos/samegame/samegame

samegame should be running now, and you can interact with it if you have a mouse connected to the raspberry pi.