Raspberry Pi Beginners Guide: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (→‎Getting Started: Replace <code> blocks with <syntaxhighlight> blocks)
 
(62 intermediate revisions by 23 users not shown)
Line 1: Line 1:
h1. Beginner's guide to cross-compile Qt5 on RaspberryPi
[[Category:Raspberry Pi]]
{{LangSwitch}}
== Introduction ==
 
This is a step by step guide to help beginners to cross-compile Qt5 for the Raspberry Pi. This allows you to be able to compile Qt5 applications for the Raspberry Pi from a PC and also deploy to the device automatically and debug programs. The instructions were tested on Ubuntu 15.04 but should work with any Debian based operating system.
 
This article will focus on simplicity, if you are looking for additional customization, please check a more [http://wiki.qt.io/RaspberryPi detailed guide].
 
For a detailed tutorial on cross-compiling under Windows, see [http://visualgdb.com/tutorials/raspberry/qt/embedded/ this tutorial].
 
For an up-to-date guide for cross-compiling Qt 5.6 targeting eglfs and Raspbian Jessie, see [[RaspberryPi2EGLFS]].
 
== Getting Started ==
 
First we create a folder to hold the source code and all the required files to do the cross-compilation. We chose to create a directory named "opt" in the home folder:
 
<syntaxhighlight lang=bash>mkdir ~/opt
 
cd ~/opt</syntaxhighlight >
 
Now, download the following files:
* Raspbian Wheezy image from ([http://www.raspberrypi.org/downloads here]):
<syntaxhighlight lang="bash">
wget http://downloads.raspberrypi.org/raspbian_latest -O wheezy-raspbian-latest.zip
 
unzip wheezy-raspbian-latest.zip</syntaxhighlight>
 
And mount it:
<syntaxhighlight  lang="bash">sudo mkdir /mnt/rasp-pi-rootfs
 
sudo mount -o loop,offset=62914560 2015-05-05-raspbian-wheezy.img /mnt/rasp-pi-rootfs</syntaxhighlight >
 
Note: The name of the image file and the offset value in the above mount command will change with the latest version of Raspbian.  To get the correct offset value, use:
 
<syntaxhighlight  lang="bash"> sudo fdisk -l /path/to/imagefile</syntaxhighlight >
 
You will see something like:
 
<syntaxhighlight lang="bash">sudo fdisk -l ./2016-09-23-raspbian-jessie.img</syntaxhighlight>
<blockquote style="background:lightgrey">
Disk ./2016-09-23-raspbian-jessie.img: 4.1 GiB, 4348444672 bytes, 8493056 sectors
 
Units: sectors of 1 * 512 = 512 bytes
 
Sector size (logical/physical): 512 bytes / 512 bytes
 
I/O size (minimum/optimal): 512 bytes / 512 bytes
 
Disklabel type: dos
 
Disk identifier: 0xd94ffdb3
 
 
Device                            Boot  Start    End Sectors Size Id Type
 
./2016-09-23-raspbian-jessie.img1        8192  137215  129024  63M  c W95 FAT32 (LBA)
 
./2016-09-23-raspbian-jessie.img2      137216 8493055 8355840  4G 83 Linux</blockquote>
 
The first image is the bootable sector.  The second image is what you want to mount.  Obtain the correct offset by multiplying the start of the second sector (here 137216) by the sector size (here 512) to obtain the offset.  Here the correct offset is '''137216 * 512 = 70254592'''
 
* Download the cross compiling toolchain:
<syntaxhighlight lang="bash">wget https://www.dropbox.com/s/sl919ly0q79m1e6/gcc-4.7-linaro-rpi-gnueabihf.tbz
 
(or at http://de.sourceforge.jp/projects/sfnet_rfidmonitor/downloads/crosscompilation-resources/gcc-4.7-linaro-rpi-gnueabihf.tbz)
 
tar -xf gcc-4.7-linaro-rpi-gnueabihf.tbz</syntaxhighlight >
 
It is a 32-bit build so if you are running on a 64-bit machine (you probably are) you need to install ia32-libs:
 
<syntaxhighlight lang="bash">paulo@westeros:~/opt$ sudo apt-get install ia32-libs</syntaxhighlight>
 
<big>* ia32-libs has been replaced with Multiarch. Try installing lib32z1 lib32ncurses5 lib32bz2-1.0 instead.</big>


[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
* Clone the cross-compile-tools repository:
This repository contains a script that will be used to fix some symbolic links.


== Introduction ==
<syntaxhighlight lang="bash" inline> git clone https://github.com/darius-kim/cross-compile-tools.git</syntaxhighlight >
 
* Clone and init the Qt5 repository:
 
<syntaxhighlight lang="bash">git clone git://code.qt.io/qt/qt5.git
 
cd qt5
 
./init-repository</syntaxhighlight>
 
If you're behind a firewall ./init-repository might fail. First run this command:
 
<syntaxhighlight lang="bash">sed -i 's/git:/https:git./' .gitmodules</syntaxhighlight>
 
Then retry:


This is a step by step guide to help beginners to: cross-compile QT5 for RaspberryPi(wheezy), create, deploy and run an example project. This article will focus on simplicity, so if you are looking for additional customization, please check a more &quot;detailed guide&amp;quot;:http://wiki.qt.io/RaspberryPi.
<syntaxhighlight lang="bash">./init-repository -f</syntaxhighlight>


For an easy install, try &quot;this script.&quot;:https://gist.github.com/raw/3488286/d1d301946297be999d96df632a0ad0095a4d77e7/bakeqtpi.bash<br />Or get a later version &quot;here&amp;quot;:http://gitorious.org/bakeqtpi<br />For help, run
== Compiling qtbase ==
Now the we have all the resources needed to compile qt5 for raspberry, we need to execute a script to fix symlinks and lib paths:


<code> $ ./bakeqtpi.bash —help<code>
<code lang="bash">cd ~/opt/cross-compile-tools


== Getting Started ==
sudo ./fixQualifiedLibraryPaths /mnt/rasp-pi-rootfs/ ~/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc</code>


First we create a folder to hold the source code and all the required files to do the cross-compilation. We chose to create a directory named &quot;opt&amp;quot; in the home folder:<br /></code>paulo</code>westeros:<sub>$ mkdir</sub>/opt<br />paulo<code>westeros:<sub>$ cd</sub>/opt<code>
Go to the qt5/qtbase folder and run:


Now, download the following files:<br />* Raspbian Wheezy image from (&quot;here&amp;quot;:http://www.raspberrypi.org/downloads):<br /></code>paulo</code>westeros:<sub>/opt$ wget http://downloads.raspberrypi.org/raspbian_latest/ -O wheezy-raspbian-latest.zip<br />paulo<code>westeros:</sub>/opt$ unzip wheezy-raspbian-latest.zip<code><br />And mount it:<br /></code>paulo</code>westeros:<sub>/opt$ sudo mkdir /mnt/rasp-pi-rootfs<br />paulo<code>westeros:</sub>/opt$ sudo mount -o loop,offset=62914560 2015-02-16-raspbian-wheezy.img /mnt/rasp-pi-rootfs<code>
<code lang="bash">./configure -opengl es2 -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=~/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf- -sysroot /mnt/rasp-pi-rootfs -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -prefix /usr/local/qt5pi -hostprefix /usr/local/qt5pi


* The toolchain built by Donald: (Note, seems to be dead links!)<br /></code>paulo</code>westeros:<sub>/opt$ wget https://www.dropbox.com/s/sl919ly0q79m1e6/gcc-4.7-linaro-rpi-gnueabihf.tbz<br />(or a mirror at http://swap.tsmt.eu/gcc-4.7-linaro-rpi-gnueabihf.tbz with sha1 f6255c6aca925239dc943d22a794f642daa17e65)<br />(or better at http://de.sourceforge.jp/projects/sfnet_rfidmonitor/downloads/crosscompilation-resources/gcc-4.7-linaro-rpi-gnueabihf.tbz<br />as the above noted links seem to be broken)<br />paulo<code>westeros:</sub>/opt$ tar -xf gcc-4.7-linaro-rpi-gnueabihf.tbz<code><br />Pro tip: don't build your own cross-compiler. You'll overlook something important and loose a couple of hours of your life for nothing. A suitable Raspberry Pi cross-compiler is also available here: https://github.com/raspberrypi/tools
make -j 4


It is a 32-bit build so if you are running on a 64-bit machine (you probably are) you need to install ia32-libs:<br /></code>paulo</code>westeros:<sub>/opt$ sudo apt-get install ia32-libs</code>
sudo make install</code>
<br />* Clone the cross-compile-tools repository:<br /><code>paulo<code>westeros:</sub>/opt$ git clone git://gitorious.org/cross-compile-tools/cross-compile-tools.git<code>


* Clone and init the Qt5 repository:<br /></code>paulo</code>westeros:<sub>/opt$ git clone git://gitorious.org/qt/qt5.git<br />paulo<code>westeros:</sub>/opt$ cd qt5<br />paulo</code>westeros:<sub>/opt/qt5$ ./init-repository</code><br />If you're behind a firewall ./init-repository might fail. First run this command:<br /><code>paulo<code>westeros:</sub>/opt/qt5$ sed <s>i 's/git:/https:git./' .gitmodules<code><br />Then retry:<br /></code>paulo</code>westeros:<sub>/opt/qt5$ ./init-repository -f</code>
Note: if you want the qmlscene binary you should add -make tools to the ./configure command.
<br />* Finally, apply a patch on the qtjsbackend repository:<br /><code>paulo<code>wseteros:</sub>/opt/qt5$ cd <sub>/opt/qt5/qtjsbackend<br />paulo</code>wseteros:</sub>/opt/qt5/qtjsbackend$ git fetch https://codereview.qt.io/p/qt/qtjsbackend refs/changes/56/27256/4 &amp;&amp; git cherry-pick FETCH_HEAD</code>
<br />h2. Compiling qtbase
<br />Now the we have all the resources needed to compile qt5 for raspberry, we need to execute a script to fix symlinks and lib paths:
<br /><code>paulo<code>westeros:<sub>$ cd</sub>/opt/cross-compile-tools<br />paulo</code>westeros:<sub>$ sudo ./fixQualifiedLibraryPaths /mnt/rasp-pi-rootfs/</sub>/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc</code>
<br />Go to the qt5/qtbase folder and run:<br /><code>paulo<code>westeros:<sub>/opt/qt5/qtbase$ ./configure -opengl es2 -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=</sub>/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf</s> -sysroot /mnt/rasp-pi-rootfs -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -prefix /usr/local/qt5pi<br />paulo</code>westeros:<sub>/opt/qt5/qtbase$ make -j 4<br />paulo<code>westeros:</sub>/opt/qt5/qtbase$ sudo make install<code><br />Note: if you want the qmlscene binary you should add -make tools to the ./configure command.


== Compiling other modules ==
== Compiling other modules ==


Now that you have qmake, you can cross-compile the other modules. To avoid dependencies errors, the following order is sugested:<br />qtimageformats, qtsvg, qtjsbackend, qtscript, qtxmlpatterns, qtdeclarative, qtsensors, qt3d, qtgraphicaleffects, qtjsondb, qtlocation, qtdocgallery.
Now that you have qmake, you can cross-compile the other modules. To avoid dependencies errors, the following order is sugested:
qtimageformats, qtsvg, qtjsbackend, qtscript, qtxmlpatterns, qtdeclarative, qtsensors, qt3d, qtgraphicaleffects, qtjsondb, qtlocation, qtdocgallery.
 
To compile, you must do like the example below:
 
<code lang="bash">paulo@westeros:~/opt/qt5$ cd qtimageformats
 
paulo@westeros:~/opt/qt5/qtimageformats$ /usr/local/qt5pi/bin/qmake .
 
paulo@westeros:~/opt/qt5/qtimageformats$ make -j4
 
paulo@westeros:~/opt/qt5/qtimageformats$ sudo make install</code>
 


To compile, you must do like the example below:<br /></code>paulo</code>westeros:<sub>/opt/qt5$ cd qtimageformats<br />paulo<code>westeros:</sub>/opt/qt5/qtimageformats$ /usr/local/qt5pi/bin/qmake .<br />paulo</code>westeros:<sub>/opt/qt5/qtimageformats$ make -j4<br />paulo<code>westeros:</sub>/opt/qt5/qtimageformats$ sudo make install<code>
== Transferring the changed image back onto SD Card ==
After completing the these instructions the Raspberry Pi image will have QT5 installed onto it now we can copy it to the sdcard using dd:


We have all the modules installed in the wheezy image, now we can copy it to the sdcard using dd:<br /></code>paulo</code>westeros:<sub>/opt/qt5$ cd</sub>/opt/<br />paulo<code>westeros:<sub>/opt$ sync; sudo umount /mnt/rasp-pi-rootfs<br />paulo</code>westeros:</sub>/opt$ sudo dd bs=1M if=2012-07-15-wheezy-raspbian.img of=/dev/sdb; sync<br /><code>
<code lang="bash">paulo@westeros:~/opt/qt5$ cd~/opt/


obs: Check the entry point of your sdcard. Mine was /dev/sdb.
paulo@westeros:~/opt$ sync; sudo umount /mnt/rasp-pi-rootfs


'''Notes for OSX'''
paulo@westeros:~/opt$ sudo dd bs=1M if=2015-05-05-raspbian-wheezy.img of=/dev/sdb; sync


* Because your sdcard will be partitioned, OSX won't let you umount it, so you'll need to type:<br /></code>sudo diskutil umountDisk /dev/disk3</code>
</code>


where &quot;disk3&amp;quot; will be the /dev/diskXsY where your sdcard first appeared.
Note: Check the what the device name of your SD Card is by running the following command which will list the storage devices on your computer. Don't use the number at the end as that is the partition number and we're writing to the whole SD card:


* To perform dd you will need to use the *r*disk index, so if your sdcard is disk3 you will need to type:<br /><code>sudo dd bs=1M if=2012-07-15-wheezy-raspbian.img of=/dev/rdisk3; sync<code>
<code lang="bash">sudo fdisk -l</code>
 
'''Notes for macOS'''
 
* Because your sdcard will be partitioned, macOS won't let you umount it, so you'll need to type:
 
<code lang="bash">sudo diskutil umountDisk /dev/disk3</code>
 
where "disk3" will be the /dev/diskXsY where your sdcard first appeared.
 
* To perform dd you will need to use the *r*disk index, so if your sdcard is disk3 you will need to type:
 
<code lang="bash">sudo dd bs=1M if=2015-05-05-raspbian-wheezy.img of=/dev/rdisk3; sync</code>


Boot up your pi.
Boot up your pi.
Line 51: Line 158:
== Configuring Qt Creator ==
== Configuring Qt Creator ==


To use Qt Creator properly, you must configure the &quot;Qt Version&amp;quot; and &quot;Tool Chains&amp;quot; sections.
To use Qt Creator properly, you must configure the "Qt Version" and "Tool Chains" sections.


=== Tool Chain Settings ===
=== Tool Chain Settings ===


go to '''Tools <s>&gt; Options..</s>&gt; Build &amp; Run -&gt; Compilers''' tab.<br />click in &quot;'''Add -&gt; GCC'''&quot;.<br />On &quot;'''Compiler Path'''&quot; set to &quot;/home/&amp;lt;you&amp;gt;/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g+''&quot;.<br />Name it &quot;ARM GCC&amp;quot; or similar.<br />obs: Adjust it to your username.
go to '''Tools -> Options..-> Build & Run -> Compilers''' tab.
<br />h3. Qt Version settings.
 
<br />Now go to '''Tools <s>&gt; Options..</s>&gt; Build &amp; Run -&gt; Qt Versions''' tab.<br />Click in &quot;Add..&quot; and choose you qmake for raspberry &quot;/usr/local/qt5pi/bin/qmake&amp;quot;.
click in "'''Add -> GCC'''".
<br />'''Note for OSX'''<br />* Because /usr is not visible by default, you first need to open a terminal and type:<br /></code>sudo SetFile -a v /usr</code><br />* After you've completed the qmake selection, if you wish to make /usr invisible again, type the same command with a capital V :<br /><code>sudo SetFile -a V /usr<code>
 
<br />h3. Configure Linux Devices
On "'''Compiler Path'''" set to "''/home/<you>/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g+''".
<br />Go to '''Tools <s>&gt; Options…</s>&gt; Devices -&gt; Devices''' tab.
 
<br />Add a new configuration setting a &quot;Generic Linux Device&amp;quot;<br />Hostname or IP address is the IP of your raspberry<br />user is '''pi'''<br />password is '''raspberry'''<br />obs: if your not did change it.
Name it "ARM GCC" or similar.
<br />Now you are ready to run your first Qt raspberry pi project.
 
<br />h2. Example Project
obs: Adjust it to your username.
<br />You can download a hello world project:<br /></code>paulo</code>westeros:<sub>$ git clone https://git.gitorious.org/qt5-raspberrypi-example/qt5-raspberrypi-example.git</code>
 
<br />Open the project in Qt Creator and go to the Projects pane.<br />Click Manage Kits and then Add.<br />Fill in:<br />Name: Raspberry Pi<br />Device Type: Generic Linux Device<br />Device: Raspberry Pi<br />Compiler: ARM GCC<br />Debugger-&gt;Manage-&gt;Edit-&gt;&quot;/home/&amp;lt;you&amp;gt;/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gdb&amp;quot; (change your username)<br />Qt version: Qt 5.x.x (qt5pi)<br />Ok-&gt;Add Kit-&gt;Raspberry Pi
=== Qt Version settings. ===
<br />Then go to the &quot;Rasperry Pi&amp;quot; kit tab-&gt;System Environment and add LD_LIBRARY_PATH which is set to /usr/local/qt5pi/lib
Now go to '''Tools -> Options..-> Build & Run -> Qt Versions''' tab.
<br />Now you can build and run your application from Qt Creator.
 
<br />h2. Troubleshooting
Click in "Add.." and choose you qmake for raspberry "/usr/local/qt5pi/bin/qmake".
<br />h3. Qt version not properly installed
 
<br />You need to keep the SD card image mounted during development!
'''Note for macOS'''
<br />If QtCreator still reports this after mounting the SD card image, then you may need to create the directories missing from  
* Because /usr is not visible by default, you first need to open a terminal and type:
<br /><code>/mnt/rasp-pi-rootfs/usr/local/qt5pi/<code>
 
<br />or whatever the mount path is (these will probably include bin, translations, and so on). You can create empty directories using the mkdir command.
<code lang="bash">sudo SetFile -a v /usr</code>
<br />h3. The default mkspec symlink is broken.
 
<br />It means that mkspec is not found on your sysroot, so your must copy it.
* After you've completed the qmake selection, if you wish to make /usr invisible again, type the same command with a capital V :
<br /></code>paulo</code>westeros:</sub>$ sudo cp -r /usr/local/qt5pi/mkspecs/ /mnt/rasp-pi-rootfs/usr/local/qt5pi/</code>
 
<br />If the problem persists, create the missing symlink, e.g.
<code lang="bash">sudo SetFile -a V /usr</code>
<br /><code>sudo ln -s /usr/local/qt5pi/mkspecs/devices/linux-rasp-pi-g''+ /usr/local/qt5pi/mkspecs/default<code>
 
An alternative is to use CMD+SHIFT+. (dot) in the file dialog to make invisible directories momentarily visible
 
=== Configure Linux Devices ===
Go to '''Tools -> Options…-> Devices -> Devices''' tab.
 
Add a new configuration setting a "Generic Linux Device"
 
Hostname or IP address is the IP of your raspberry
 
user is '''pi'''
 
password is '''raspberry'''
 
obs: if your not did change it.
 
== Target Path ==
When you try to deploy a program on your Raspberry Pi from Qtcreator you may get an error about an unknown command. This is because Qtcreator does not know where to copy the compiled file onto the Raspberry Pi.
 
The following line should be added to your .pro file:
 
<code lang="bash">target.path = /home/pi/</code>
 
You may change the path to anything you want and the executable will be copied to and run from this location.
 
 
Now you are ready to run your first Qt raspberry pi project.
 
== Example Project ==
You can download a hello world project:
 
<code lang="bash">paulo@westeros:~$ git clone https://git.gitorious.org/qt5-raspberrypi-example/qt5-raspberrypi-example.git</code>
 
Open the project in Qt Creator and go to the Projects pane.
 
Click Manage Kits and then Add.
 
Fill in:
 
Name: Raspberry Pi
 
Device Type: Generic Linux Device
 
Device: Raspberry Pi
 
Compiler: ARM GCC
 
Debugger->Manage->Edit->"/home/<you>/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gdb" (change your username)
 
Qt version: Qt 5.x.x (qt5pi)
 
Ok->Add Kit->Raspberry Pi
 
Then go to the "Rasperry Pi" kit tab->System Environment and add LD_LIBRARY_PATH which is set to /usr/local/qt5pi/lib
 
Now you can build and run your application from Qt Creator.
 
== Troubleshooting ==
=== Qt version not properly installed ===
You need to keep the SD card image mounted during development!
 
Use this command in the same folder as the .img file:
 
<code lang="bash">sudo mount -o loop,offset=62914560 2015-05-05-raspbian-wheezy.img /mnt/rasp-pi-rootfs</code>
 
If QtCreator still reports this after mounting the SD card image, then you may need to create the directories missing from  
 
<code lang="bash">/mnt/rasp-pi-rootfs/usr/local/qt5pi/</code>
 
or whatever the mount path is (these will probably include bin, translations, and so on). You can create empty directories using the mkdir command.
 
=== The default mkspec symlink is broken. ===
It means that mkspec is not found on your sysroot, so your must copy it.
 
<code lang="bash">paulo@westeros:~$ sudo cp -r /usr/local/qt5pi/mkspecs/ /mnt/rasp-pi-rootfs/usr/local/qt5pi/</code>
 
If the problem persists, create the missing symlink, e.g.
 
<code lang="bash">sudo ln -s /usr/local/qt5pi/mkspecs/devices/linux-rasp-pi-g''+ /usr/local/qt5pi/mkspecs/default</code>


It is probably a good idea to do the same for the sysroot mkspecs.
It is probably a good idea to do the same for the sysroot mkspecs.


=== Include &lt;QtGui/QtGui&amp;gt;: No such file or directory ===
=== Include <QtGui/QtGui>: No such file or directory ===


It seems Qt's include was not found and you must add it to you .pro file:
It seems Qt's include was not found and you must add it to you .pro file:


</code>INCLUDEPATH ''= /mnt/rasp-pi-rootfs/usr/local/qt5pi/include/</code>
<code>INCLUDEPATH += /mnt/rasp-pi-rootfs/usr/local/qt5pi/include/</code>
<br />h3. cc1: fatal error: .pch/release-shared/QtGui: No such file or directory
<br />Some people have seen this error. Adding the &quot;-no-pch&amp;quot; option when running configure should prevent it.
<br />h3. Getting Could not determine the target architecture! and Could not determine the host architecture!
<br />If you are getting this kind of error, it is possible that your current git branch is something other than master. To be sure, do the following:
<br /><code>daniel<code>daniel-t3500 : <sub>/opt/qt5/qtbase $ git branch <code>
<br />If it shows like this:<br /></code>* (no branch)<br /> master<code>
<br />Then you must checkout to master branch before running ./configure:<br /></code>daniel</code>daniel-t3500 :</sub>/opt/qt5/qtbase $ git checkout master<br />daniel<code>daniel-t3500 : <sub>/opt/qt5/qtbase $ git branch<br />* master</code>
<br />It is also good to fetch the latest commits and apply them by doing:<br /><code>daniel<code>daniel-t3500 :</sub>/opt/qt5/qtbase $ git pull<code>
<br />Also, check for the installation of libz (32 bits) in yor system. In ubuntu (64 bits) you might just:<br /></code>sudo apt-get install lib32z1-dev</code>
<br />After the ./configure script has failed first run these commands before restarting ./configure:
<br /><code>daniel<code>daniel-t3500 : <sub>/opt/qt5/qtbase $ git reset —hard<br />daniel</code>daniel-t3500 :</sub>/opt/qt5/qtbase $ git git clean -dxf</code>
<br />h3. EGL Error : Could not create the egl surface: error = 0x3000
<br />Try allocating more memory to video (try 64 or 128MB). Use the raspi-config utility to change this.
<br /><code>pi<code>raspberrypi ~ $ sudo raspi-config<code>
<br />h3. ABI detection failed
<br />You will need Qt Creator 2.6 for Qt5 up to and including beta2.<br />Any Qt 5 later than that will need to use nightly snapshots (or will have to wait for Qt Creator 2.6.1): After the beta2 all libraries were renamed again.
<br />See &quot;this forum thread for more details&amp;quot;:http://forum.qt.io/viewthread/22023
<br />h3. ./configure: 2654: ./configure: /home/&amp;lt;you&amp;gt;/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g: not found -reduce-exports was requested but this compiler does not support it
<br />The error message is totally misleading. The real cause is that the precompile toolchain is 32-bit, so you need to:<br /></code>sudo apt-get install ia32-libs</code>
<br />h3. make: g: Command not found
<br />Qt requires a native (non-cross-compiling) copy of g''+ to be installed in order to build qmake. You can't use clang. Fix with:<br /><code>sudo apt-get install g++<code>


=== /mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libdl.a(dlopen.o): In function `dlopen': (.text+0xc): undefined reference to `__dlopen' ===
=== cc1: fatal error... ===
'''cc1: fatal error: .pch/release-shared/QtGui: No such file or directory'''
 
Some people have seen this error. Adding the "-no-pch" option when running configure should prevent it.
 
=== Getting Could not determine... ===
'''Getting Could not determine the target architecture! and Could not determine the host architecture!'''
 
If you are getting this kind of error, it is possible that your current git branch is something other than master. To be sure, do the following:
 
<code lang="bash">daniel@daniel-t3500 : ~/opt/qt5/qtbase $ git branch </code>
 
If it shows like this:
<code>* (no branch)
master</code>
 
Then you must checkout to master branch before running ./configure:
 
<code lang="bash">daniel@daniel-t3500 :~/opt/qt5/qtbase $ git checkout master
 
daniel@daniel-t3500 : ~/opt/qt5/qtbase $ git branch
 
* master</code>
 
It is also good to fetch the latest commits and apply them by doing:
 
<code lang="bash">daniel@daniel-t3500 :~/opt/qt5/qtbase $ git pull</code>
 
Also, check for the installation of libz (32 bits) in yor system. In ubuntu (64 bits) you might just:
 
<code lang="bash">sudo apt-get install lib32z1-dev</code>
 
After the ./configure script has failed first run these commands before restarting ./configure:
 
<code lang="bash">daniel@daniel-t3500 : ~/opt/qt5/qtbase $ git reset —hard
 
daniel@daniel-t3500 :~/opt/qt5/qtbase $ git git clean -dxf</code>
 
=== EGL Error : Could not create... ===
'''EGL Error : Could not create the egl surface: error = 0x3000'''
 
Try allocating more memory to video (try 64 or 128MB). Use the raspi-config utility to change this.
 
<code lang="bash">pi@raspberrypi ~ $ sudo raspi-config</code>
 
=== ABI detection failed ===
You will need Qt Creator 2.6 for Qt5 up to and including beta2.
 
Any Qt 5 later than that will need to use nightly snapshots (or will have to wait for Qt Creator 2.6.1): After the beta2 all libraries were renamed again.
 
See [http://forum.qt.io/viewthread/22023 this forum thread for more details]
 
=== ./configure: 2654: ./configure... ===
'''./configure: 2654: ./configure: /home/<you>/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g: not found -reduce-exports was requested but this compiler does not support it'''
 
The error message is totally misleading. The real cause is that the precompile toolchain is 32-bit, so you need to:
 
<code lang="bash">sudo apt-get install ia32-libs</code>
 
=== make: g: Command not found ===
Qt requires a native (non-cross-compiling) copy of ''g++'' to be installed in order to build qmake. You can't use clang. Fix with:
 
<code lang="bash">sudo apt-get install build-essential</code>
 
=== In function `dlopen'... ===
'''/mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libdl.a(dlopen.o): In function `dlopen': (.text+0xc): undefined reference to `__dlopen' '''


You need to run the ./fixQualifiedLibraryPaths again.
You need to run the ./fixQualifiedLibraryPaths again.
Line 115: Line 344:
See: http://stackoverflow.com/questions/13626726/an-error-building-qt-libraries-for-the-raspberry-pi for more info.
See: http://stackoverflow.com/questions/13626726/an-error-building-qt-libraries-for-the-raspberry-pi for more info.


=== cc1: fatal error: .pch/release-shared/Qt5Gui: No such file or directory ===
=== cc1: fatal error... ===
'''cc1: fatal error: .pch/release-shared/Qt5Gui: No such file or directory'''


Try adding -no-pch to the ./configure command line.
Try adding -no-pch to the ./configure command line.


=== Cannot create forwarding script Betrete 'qtactiveqt'.git/hooks/commit-msg: Datei oder Verzeichnis nicht gefunden ===
=== Cannot create forwarding script... ===
'''Cannot create forwarding script Betrete 'qtactiveqt'.git/hooks/commit-msg: Datei oder Verzeichnis nicht gefunden'''
 
Try:
 
<code lang="bash">unset LANG


Try:<br /></code>unset LANG<br />./init-repository -f<code>
./init-repository -f</code>

Latest revision as of 04:50, 7 June 2023

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Introduction

This is a step by step guide to help beginners to cross-compile Qt5 for the Raspberry Pi. This allows you to be able to compile Qt5 applications for the Raspberry Pi from a PC and also deploy to the device automatically and debug programs. The instructions were tested on Ubuntu 15.04 but should work with any Debian based operating system.

This article will focus on simplicity, if you are looking for additional customization, please check a more detailed guide.

For a detailed tutorial on cross-compiling under Windows, see this tutorial.

For an up-to-date guide for cross-compiling Qt 5.6 targeting eglfs and Raspbian Jessie, see RaspberryPi2EGLFS.

Getting Started

First we create a folder to hold the source code and all the required files to do the cross-compilation. We chose to create a directory named "opt" in the home folder:

mkdir ~/opt

cd ~/opt

Now, download the following files:

  • Raspbian Wheezy image from (here):
wget http://downloads.raspberrypi.org/raspbian_latest -O wheezy-raspbian-latest.zip

unzip wheezy-raspbian-latest.zip

And mount it:

sudo mkdir /mnt/rasp-pi-rootfs

sudo mount -o loop,offset=62914560 2015-05-05-raspbian-wheezy.img /mnt/rasp-pi-rootfs

Note: The name of the image file and the offset value in the above mount command will change with the latest version of Raspbian. To get the correct offset value, use:

 sudo fdisk -l /path/to/imagefile

You will see something like:

sudo fdisk -l ./2016-09-23-raspbian-jessie.img

Disk ./2016-09-23-raspbian-jessie.img: 4.1 GiB, 4348444672 bytes, 8493056 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0xd94ffdb3


Device Boot Start End Sectors Size Id Type

./2016-09-23-raspbian-jessie.img1 8192 137215 129024 63M c W95 FAT32 (LBA)

./2016-09-23-raspbian-jessie.img2 137216 8493055 8355840 4G 83 Linux

The first image is the bootable sector. The second image is what you want to mount. Obtain the correct offset by multiplying the start of the second sector (here 137216) by the sector size (here 512) to obtain the offset. Here the correct offset is 137216 * 512 = 70254592

  • Download the cross compiling toolchain:
wget https://www.dropbox.com/s/sl919ly0q79m1e6/gcc-4.7-linaro-rpi-gnueabihf.tbz

(or at http://de.sourceforge.jp/projects/sfnet_rfidmonitor/downloads/crosscompilation-resources/gcc-4.7-linaro-rpi-gnueabihf.tbz)

tar -xf gcc-4.7-linaro-rpi-gnueabihf.tbz

It is a 32-bit build so if you are running on a 64-bit machine (you probably are) you need to install ia32-libs:

paulo@westeros:~/opt$ sudo apt-get install ia32-libs

* ia32-libs has been replaced with Multiarch. Try installing lib32z1 lib32ncurses5 lib32bz2-1.0 instead.

  • Clone the cross-compile-tools repository:

This repository contains a script that will be used to fix some symbolic links.

git clone https://github.com/darius-kim/cross-compile-tools.git

  • Clone and init the Qt5 repository:
git clone git://code.qt.io/qt/qt5.git

cd qt5

./init-repository

If you're behind a firewall ./init-repository might fail. First run this command:

sed -i 's/git:/https:git./' .gitmodules

Then retry:

./init-repository -f

Compiling qtbase

Now the we have all the resources needed to compile qt5 for raspberry, we need to execute a script to fix symlinks and lib paths:

cd ~/opt/cross-compile-tools

sudo ./fixQualifiedLibraryPaths /mnt/rasp-pi-rootfs/ ~/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc

Go to the qt5/qtbase folder and run:

./configure -opengl es2 -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=~/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf- -sysroot /mnt/rasp-pi-rootfs -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -prefix /usr/local/qt5pi -hostprefix /usr/local/qt5pi

make -j 4

sudo make install

Note: if you want the qmlscene binary you should add -make tools to the ./configure command.

Compiling other modules

Now that you have qmake, you can cross-compile the other modules. To avoid dependencies errors, the following order is sugested: qtimageformats, qtsvg, qtjsbackend, qtscript, qtxmlpatterns, qtdeclarative, qtsensors, qt3d, qtgraphicaleffects, qtjsondb, qtlocation, qtdocgallery.

To compile, you must do like the example below:

paulo@westeros:~/opt/qt5$ cd qtimageformats

paulo@westeros:~/opt/qt5/qtimageformats$ /usr/local/qt5pi/bin/qmake .

paulo@westeros:~/opt/qt5/qtimageformats$ make -j4

paulo@westeros:~/opt/qt5/qtimageformats$ sudo make install


Transferring the changed image back onto SD Card

After completing the these instructions the Raspberry Pi image will have QT5 installed onto it now we can copy it to the sdcard using dd:

paulo@westeros:~/opt/qt5$ cd~/opt/

paulo@westeros:~/opt$ sync; sudo umount /mnt/rasp-pi-rootfs

paulo@westeros:~/opt$ sudo dd bs=1M if=2015-05-05-raspbian-wheezy.img of=/dev/sdb; sync

Note: Check the what the device name of your SD Card is by running the following command which will list the storage devices on your computer. Don't use the number at the end as that is the partition number and we're writing to the whole SD card:

sudo fdisk -l

Notes for macOS

  • Because your sdcard will be partitioned, macOS won't let you umount it, so you'll need to type:
sudo diskutil umountDisk /dev/disk3

where "disk3" will be the /dev/diskXsY where your sdcard first appeared.

  • To perform dd you will need to use the *r*disk index, so if your sdcard is disk3 you will need to type:
sudo dd bs=1M if=2015-05-05-raspbian-wheezy.img of=/dev/rdisk3; sync

Boot up your pi.

Configuring Qt Creator

To use Qt Creator properly, you must configure the "Qt Version" and "Tool Chains" sections.

Tool Chain Settings

go to Tools -> Options..-> Build & Run -> Compilers tab.

click in "Add -> GCC".

On "Compiler Path" set to "/home/<you>/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g+".

Name it "ARM GCC" or similar.

obs: Adjust it to your username.

Qt Version settings.

Now go to Tools -> Options..-> Build & Run -> Qt Versions tab.

Click in "Add.." and choose you qmake for raspberry "/usr/local/qt5pi/bin/qmake".

Note for macOS

  • Because /usr is not visible by default, you first need to open a terminal and type:
sudo SetFile -a v /usr
  • After you've completed the qmake selection, if you wish to make /usr invisible again, type the same command with a capital V :
sudo SetFile -a V /usr

An alternative is to use CMD+SHIFT+. (dot) in the file dialog to make invisible directories momentarily visible

Configure Linux Devices

Go to Tools -> Options…-> Devices -> Devices tab.

Add a new configuration setting a "Generic Linux Device"

Hostname or IP address is the IP of your raspberry

user is pi

password is raspberry

obs: if your not did change it.

Target Path

When you try to deploy a program on your Raspberry Pi from Qtcreator you may get an error about an unknown command. This is because Qtcreator does not know where to copy the compiled file onto the Raspberry Pi.

The following line should be added to your .pro file:

target.path = /home/pi/

You may change the path to anything you want and the executable will be copied to and run from this location.


Now you are ready to run your first Qt raspberry pi project.

Example Project

You can download a hello world project:

paulo@westeros:~$ git clone https://git.gitorious.org/qt5-raspberrypi-example/qt5-raspberrypi-example.git

Open the project in Qt Creator and go to the Projects pane.

Click Manage Kits and then Add.

Fill in:

Name: Raspberry Pi

Device Type: Generic Linux Device

Device: Raspberry Pi

Compiler: ARM GCC

Debugger->Manage->Edit->"/home/<you>/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gdb" (change your username)

Qt version: Qt 5.x.x (qt5pi)

Ok->Add Kit->Raspberry Pi

Then go to the "Rasperry Pi" kit tab->System Environment and add LD_LIBRARY_PATH which is set to /usr/local/qt5pi/lib

Now you can build and run your application from Qt Creator.

Troubleshooting

Qt version not properly installed

You need to keep the SD card image mounted during development!

Use this command in the same folder as the .img file:

sudo mount -o loop,offset=62914560 2015-05-05-raspbian-wheezy.img /mnt/rasp-pi-rootfs

If QtCreator still reports this after mounting the SD card image, then you may need to create the directories missing from

/mnt/rasp-pi-rootfs/usr/local/qt5pi/

or whatever the mount path is (these will probably include bin, translations, and so on). You can create empty directories using the mkdir command.

The default mkspec symlink is broken.

It means that mkspec is not found on your sysroot, so your must copy it.

paulo@westeros:~$ sudo cp -r /usr/local/qt5pi/mkspecs/ /mnt/rasp-pi-rootfs/usr/local/qt5pi/

If the problem persists, create the missing symlink, e.g.

sudo ln -s /usr/local/qt5pi/mkspecs/devices/linux-rasp-pi-g''+ /usr/local/qt5pi/mkspecs/default

It is probably a good idea to do the same for the sysroot mkspecs.

Include <QtGui/QtGui>: No such file or directory

It seems Qt's include was not found and you must add it to you .pro file:

INCLUDEPATH += /mnt/rasp-pi-rootfs/usr/local/qt5pi/include/

cc1: fatal error...

cc1: fatal error: .pch/release-shared/QtGui: No such file or directory

Some people have seen this error. Adding the "-no-pch" option when running configure should prevent it.

Getting Could not determine...

Getting Could not determine the target architecture! and Could not determine the host architecture!

If you are getting this kind of error, it is possible that your current git branch is something other than master. To be sure, do the following:

daniel@daniel-t3500 : ~/opt/qt5/qtbase $ git branch

If it shows like this:

* (no branch)
 master

Then you must checkout to master branch before running ./configure:

daniel@daniel-t3500 :~/opt/qt5/qtbase $ git checkout master

daniel@daniel-t3500 : ~/opt/qt5/qtbase $ git branch

* master

It is also good to fetch the latest commits and apply them by doing:

daniel@daniel-t3500 :~/opt/qt5/qtbase $ git pull

Also, check for the installation of libz (32 bits) in yor system. In ubuntu (64 bits) you might just:

sudo apt-get install lib32z1-dev

After the ./configure script has failed first run these commands before restarting ./configure:

daniel@daniel-t3500 : ~/opt/qt5/qtbase $ git reset —hard

daniel@daniel-t3500 :~/opt/qt5/qtbase $ git git clean -dxf

EGL Error : Could not create...

EGL Error : Could not create the egl surface: error = 0x3000

Try allocating more memory to video (try 64 or 128MB). Use the raspi-config utility to change this.

pi@raspberrypi ~ $ sudo raspi-config

ABI detection failed

You will need Qt Creator 2.6 for Qt5 up to and including beta2.

Any Qt 5 later than that will need to use nightly snapshots (or will have to wait for Qt Creator 2.6.1): After the beta2 all libraries were renamed again.

See this forum thread for more details

./configure: 2654: ./configure...

./configure: 2654: ./configure: /home/<you>/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g: not found -reduce-exports was requested but this compiler does not support it

The error message is totally misleading. The real cause is that the precompile toolchain is 32-bit, so you need to:

sudo apt-get install ia32-libs

make: g: Command not found

Qt requires a native (non-cross-compiling) copy of g++ to be installed in order to build qmake. You can't use clang. Fix with:

sudo apt-get install build-essential

In function `dlopen'...

/mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libdl.a(dlopen.o): In function `dlopen': (.text+0xc): undefined reference to `__dlopen'

You need to run the ./fixQualifiedLibraryPaths again.

See: http://stackoverflow.com/questions/13626726/an-error-building-qt-libraries-for-the-raspberry-pi for more info.

cc1: fatal error...

cc1: fatal error: .pch/release-shared/Qt5Gui: No such file or directory

Try adding -no-pch to the ./configure command line.

Cannot create forwarding script...

Cannot create forwarding script Betrete 'qtactiveqt'.git/hooks/commit-msg: Datei oder Verzeichnis nicht gefunden

Try:

unset LANG

./init-repository -f