From bigfug
Jump to: navigation, search

This guide is a complete list of steps for cross compiling Qt 5.9 and Fugio on a Raspberry Pi computer.

These are the steps that worked for me, but no guarantee is made that things will be exactly the same for you. Every computer is different, but hopefully there is enough help here to get you through.

Please let me know if you there's any missing or changed information in this guide.

About This Guide

Cross compiling means that we will be building the code on a Linux computer and then deploying it to the Pi, rather than compiling everything on the Pi itself.

We're taking this approach because it's proven to work, and compiling is quite a bit faster.

If you don't already have a computer running Linux, the following instructions show how to setup a virtual machine on either Windows or OS X.

This guide is aimed at those who are familiar with writing SD-CARD images for the Pi, and installing and running Ubuntu.

Several of the steps in this guide require downloading very large files, and others will take time to process. With a good internet connection, a fast computer, and some Linux skills, the whole process could take as little as 1-2 hours.

Prerequisites

You will need:

  • a Raspberry Pi - we're using a version 3 here, but it should work on a v2, perhaps even a v1, but that might be too slow
  • a fast 8GB SD-CARD for your Pi. It may work on smaller ones, but I haven't tested it
  • a computer running Windows, OS X, or Linux where we're be doing the compiling
  • a network connection between the two computers

I'd recommend a wired gigabit connection between the two computers as we'll be transferring lots of data.

Raspberry Pi Preparation

The first step will be to download and install a fresh copy of Raspbian Jessie to work with.

https://www.raspberrypi.org/downloads/raspbian/

Download the full desktop image (this guide is using the May 2016 version)

Burn this image to an SD-CARD:

https://www.raspberrypi.org/documentation/installation/installing-images/

Boot up the Pi, run a terminal and type:

sudo raspi-config

Go to Advanced Settings

Go to Memory Split

Change the value to 256

Finish

Reboot

Install Raspberry Pi Packages

Run a terminal and type:

sudo nano /etc/apt/sources.list

On the third line, which begins with:

#deb-src ...

Remove the # character and press CTRL+X to save and exit

Enter the following commands:

sudo apt-get update
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

We'll now create a directory for the Qt libraries to go into:

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

Setting up VirtualBox

If you have a (real or virtual) Ubuntu computer already, you can skip this section.

Download the Ubuntu image:

http://www.ubuntu.com/download/desktop

Download and install VirtualBox:

https://www.virtualbox.org/

Create a new virtual machine:

  • Name: Ubuntu RPI
  • Type: Linux
  • Version: Ubuntu (64-bit)

Memory: 2048 MB

Create a virtual hard disk (VDI) Dynamically allocated Size: 64 GB

Settings

  • General
    • Advanced
      • Shared Clipboard: Enabled
  • System
    • Processor
      • Processors: 4 (or the highest your computer can use)
  • Display
    • Screen
      • Video Memory: 64 MB
      • Enable 3D Acceleration
  • Storage
    • Optical Drive
      • Choose Ubuntu disk image you just downloaded
  • Network
    • Bridged Adapter

Start the virtual machine

Install Ubuntu

Choose Install Ubuntu Download Updates Install 3rd Party Software

Erase disk and install Ubuntu Install Now Choose location Choose keyboard

Ubuntu User Settings

Your name: RPI Computer Name: RPI-DEV Username: rpi Password: rpi Login Automatically

Post Install and Restart

Open a terminal

sudo apt update
sudo apt upgrade
sudo apt install dkms

Install Guest Additions CD

Menu: VM -> Devices -> Install Guest Additions CD

Run

Eject CD

Restart

Update Ubuntu

If you didn't install a virtual machine (or have one already) do make sure it's up to date by opening a terminal and entering the following:

sudo apt update
sudo apt upgrade

Test the network connection

With both the Linux machine (real or virtual) and the Pi connected to the network, you should test they are able to talk to each other by entering the following in a terminal on the Linux machine:

ping -c 5 raspberrypi

The Pi has a hostname of raspberrypi by default, so it's probably a good idea to make sure you only have on Pi on your network at this point!

When you run the ping command you should see something along these lines:

PING raspberrypi.home (192.168.0.48): 56 data bytes
64 bytes from 192.168.0.48: icmp_seq=0 ttl=64 time=5.552 ms
64 bytes from 192.168.0.48: icmp_seq=1 ttl=64 time=8.324 ms
64 bytes from 192.168.0.48: icmp_seq=2 ttl=64 time=2.640 ms
64 bytes from 192.168.0.48: icmp_seq=3 ttl=64 time=49.048 ms
64 bytes from 192.168.0.48: icmp_seq=4 ttl=64 time=5.057 ms

--- raspberrypi.home ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 2.640/14.124/49.048/17.555 ms

If you don't see any responses, you'll need to resolve this before you can continue. Diagnosing network problems is out of the scope of this guide.

Set up SSH on Ubuntu and RPI

As we're going to copying lots of files between the two computers, we're going to set up SSH to make this process easier:

In a terminal on both the Linux machine and the Pi, type the following:

ssh-keygen -t rsa

This creates an SSH key for both machines. Doing this on the Pi is optional, but it does make sure the ~/.ssh directory exists and has the right permissions

We now need to copy the public SSH key from the Linux machine to the Pi:

cat ~/.ssh/id_rsa.pub | ssh pi@raspberrypi 'cat >> .ssh/authorized_keys'

The password for the Pi is raspberry

To make it even easier to copy files, we're going to add an entry in the SSH config file on the Linux machine:

nano ~/.ssh/config

Add the following lines to the file:

Host rpi 
        HostName raspberrypi
        User pi

Press CTRL+X to save and exit

You should now be able to type the following:

ssh rpi

The first time you do this it will ask you if you want to add this to the list of known hosts, which we do:

The authenticity of host 'raspberrypi (192.168.0.48)' can't be established.
ECDSA key fingerprint is SHA256:Dc36yemEf5YzEBHVgca2FPFZOtqwVBJj7aldyf2VKoU.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'raspberrypi,192.168.0.48' (ECDSA) to the list of known hosts.

You will then see:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Sep 15 10:00:30 2016 from bigfug7
pi@raspberrypi:~ $ 

You're successfully logged into the Pi at this point. We can just type 'exit', now that we know it's working:

pi@raspberrypi:~ $ exit
logout
Connection to raspberrypi closed.

Install Ubuntu Packages

We need to install some packages for building the software. Enter the following into a terminal on the Linux machine:

sudo apt install git libgl1-mesa-dev libglu1-mesa-dev qt5-default lib32ncurses5 lib32z1 lib32stdc++6 gdb-multiarch

Set up our Cross Compile Environment

We're going to create a directory that contains all the tools and code we need to do cross compiling.

Get the Raspberry Pi tools

On the Linux machine:

mkdir ~/raspi
cd ~/raspi
git clone https://github.com/raspberrypi/tools

This can take some time...

Create a sysroot

On the Linux machine, still in the ~/raspi directory:

mkdir sysroot sysroot/usr sysroot/opt sysroot/usr/local

We're going to create a small script that will sync the sysroot between the Pi and the Linux machine:

nano ./sysroot-sync.sh

Copy and paste the following:

#!/bin/bash

rsync -avz rpi:/lib sysroot
rsync -avz rpi:/usr/include sysroot/usr
rsync -avz rpi:/usr/lib sysroot/usr
rsync -avz rpi:/usr/local/include sysroot/usr/local
rsync -avz rpi:/usr/local/lib sysroot/usr/local
rsync -avz rpi:/opt/vc sysroot/opt

Press CTRL+X to save and exit

Make the script executable and run it:

chmod +x ./sysroot-sync.sh
./sysroot-sync.sh

Make symlinks relative

We need to adjust links within the sysroot to make sure they point to the right place. Fortunately, there is a script that does that, which can be downloaded and setup using:

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

Run it now:

./sysroot-relativelinks.py sysroot

Build and Deploy Qt

Qt is the open-source, cross-platform system that Fugio is built on. We need to build and deploy it to the Pi in order to compile Fugio.

Download Qt

On the Linux machine, still in the ~/raspi directory:

git clone git://code.qt.io/qt/qtbase.git -b 5.9

We now cd to this directory:

cd qtbase

Compile Qt

Enter the following:

./configure -release -opengl es2 -device linux-rasp-pi3-g++ -device-option CROSS_COMPILE=~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- -sysroot ~/raspi/sysroot -opensource -confirm-license -make libs -prefix /usr/local/qt5pi -extprefix ~/raspi/qt5pi -hostprefix ~/raspi/qt5 -no-use-gold-linker -v

This is the command that will configure Qt for a Raspberry Pi 3. If you want to compile it for a v2, you need to change the value after the -device option to linux-rasp-pi2-g++

For Qt 5.7 and 5.8 you have to use a device of linux-rpi3-g++

If everything looks like it worked, enter the following to build Qt:

make -j4
make install

The -j4 option enables parallel compiling using the processors of your computer, and makes compilation much quicker. The 4 matches the number of processors in your computer (or the number we set when configuring the virtual machine). If you have 8 or more cores, you can up this number to get even quicker results.

Copy Qt to the Pi

Now we have all the Qt libraries built, we need to copy them to the Pi:

cd ~/raspi
rsync -avz qt5pi rpi:/usr/local

We also need to tell the Pi where to find the libraries, so in a terminal on the Pi, enter the following:

echo /usr/local/qt5pi/lib | sudo tee /etc/ld.so.conf.d/qt5pi.conf
sudo ldconfig

Fix the OpenGL library links

While we're on the Pi, we need to fix a problem. When we setup the Pi we had to install some libraries that stop us taking advantage of the Pi's built in 3D graphics processor. Let's fix this now:

sudo rm /usr/lib/arm-linux-gnueabihf/libEGL.so.1.0.0 /usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.0.0
sudo ln -s /opt/vc/lib/libEGL.so /usr/lib/arm-linux-gnueabihf/libEGL.so.1.0.0
sudo ln -s /opt/vc/lib/libGLESv2.so /usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.0.0
sudo ln -s /opt/vc/lib/libEGL.so /opt/vc/lib/libEGL.so.1
sudo ln -s /opt/vc/lib/libGLESv2.so /opt/vc/lib/libGLESv2.so.2

Test Qt

We can now build a test example to make sure we've installed Qt properly.

On the Linux machine:

cd ~/raspi/qtbase/examples/opengl/qopenglwidget
~/raspi/qt5/bin/qmake
make
scp qopenglwidget rpi:/home/pi

On the Pi:

~/qopenglwidget

You should see a full screen window with 3D graphics running at about 60 frames per second. If not, try redoing the OpenGL library fix

Install Qt Creator

To set up the cross compiling tools on the Linux machine, we need to install Qt Creator, which is the Qt code editor (and the one I use to create Fugio with):

sudo apt install qtcreator

If you're running a native Linux machine, you can run Qt Creator with the following:

qtcreator &

However, there seems to be a problem with running it on a VirtualBox machine, so we need to do the following:

qtcreator -noload Welcome -noload QmlDesigner -noload QmlProfiler &

Configure the Cross Compiler Kit

Open the Qt Creator preferences/options window

Devices -> Add -> Generic Linux Device

Name: Raspberry Pi Host: rpi User: pi Authentication: Key

Continue

Qt Creator will test the connection to the Pi

Click Apply (if there is a button for it)

Still in the Qt Creator options:

Build & Run -> Compilers -> Add -> GCC

Name: Raspberry Pi GCC

Compiler Path -> Choose -> ~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++

Click Apply (if there is a button for it)

Build & Run -> Debuggers -> Add

Name: Raspberry Pi GDB

Path -> Choose -> ~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gdb

Click Apply (if there is a button for it)

Build & Run -> Qt Versions

Check if there is an entry for ~/raspi/qt5/bin/qmake else click Add and select it

Click Apply (if there is a button for it)

Build & Run -> Kits -> Add

Name: Raspberry Pi

Device Type: Generic Linux Device

Device: Raspberry Pi

Sysroot: ~/raspi/sysroot

Compiler: Raspberry Pi GCC

Debugger: Raspberry Pi GDB

Qt Version: The one we just added

Qt mkspec: Leave empty

Building and Deploying Fugio

We now have everything we need to be be able to build and deploy Fugio

Compile Fugio

Now we have a fully configured Qt Creator on our Linux machine, we can download and compile the Fugio source code.

On the Linux machine:

cd ~/Desktop/
mkdir Fugio
cd Fugio
git clone https://github.com/bigfug/Fugio

In Qt Creator, you can now open the ~/Desktop/Fugio/Fugio/Fugio.pro project file.

Choose the Raspberry Pi kit that we just set up.

Change the build type from debug to release (the icon on the left of the window, above the green arrows)

Click the hammer icon below the green arrows to build Fugio

Deploy Fugio

Once built, we need to copy Fugio to the Pi. This is a simple process where you choose Deploy from Qt Creator's Build menu

Currently Fugio will be deployed to the Pi's desktop

Running Fugio

On the Pi, open the Fugio folder on the desktop and double-click on the Fugio icon.

A full screen window will come up.

Fugio is currently running in EGLFS mode, which means that it is using the graphics chip of the Pi for fast 3D graphics. This has the downside that the Fugio window is forced to full screen and cannot be moved.

We can also run Fugio in the more standard windowed mode by running it with a platform option:

~/Desktop/Fugio/Fugio -platform xcb

Fugio is now in windowed mode, which operates like a standard desktop application.

Create a Fugio Desktop Entry

If you'd rather run Fugio in windowed mode, we can easily set up a desktop shortcut for it.

In a terminal, enter the following:

nano ~/Desktop/Fugio.desktop

And copy and paste:

[Desktop Entry]
Type=Application
Name=Fugio
Path=/home/pi/Desktop/Fugio
Exec=/home/pi/Desktop/Fugio/Fugio -platform xcb
Icon=/home/pi/Desktop/Fugio/share/images/fugio-icon.png

Press CTRL+X to save and exit

Make it executable:

chmod +x ~/Desktop/Fugio.desktop

You should now have an icon on your desktop that will open Fugio in windowed mode

Adding Fugio Libraries

The version of Fugio that we have built doesn't have all the required libraries built into it yet.

I've done this on purpose because as Fugio adds more functionality, you will probably want to keep it updated, rather than go through this whole process again.

Let's install the missing libraries on the Pi:

sudo apt install liblua5.2-dev libfftw3-dev libportmidi-dev

We can also build the pigpio library that is going to allow access to the Pi's GPIO hardware pins:

cd ~/Desktop
git clone https://github.com/joan2937/pigpio
cd pigpio
make -j4
sudo make install

We now need to copy these new libraries back to the Linux machine so we can compile them into Fugio. On the Linux machine:

cd ~/raspi
./sysroot-sync.sh

In Qt Creator, choose Build -> Rebuild All

Finally, choose Build -> Deploy

What works and What doesn't

Adding support for Raspberry Pi is ongoing work. Most of Fugio works fine already, but there are a couple of areas which I'm still working on:

  • PortAudio input/output doesn't work yet
  • OpenGL doesn't work yet
  • pigpio is not implemented yet

These plugins need more work to be tuned for the Pi's hardware, but they're both almost there!

Sources used in this guide

Getting Fugio compiled and running on Raspberry Pi has a been a long process, mainly because the information keeps changing. I pieced together the process largely from the RaspberryPi2EGLFS guide, updating it for Raspberry Pi v3, Qt 5.7, and adding some extra steps for SSH and helper scripts.