0% found this document useful (0 votes)
118 views

QT Cross Compile How To

This document discusses cross-compiling Qt5 applications for the Raspberry Pi 3 using Buildroot. Key steps include: 1. Downloading and configuring Buildroot for the Raspberry Pi 3. 2. Adding Qt5 support to Buildroot and compiling it, which takes around two hours. 3. Creating a GUI application in Qt Creator and specifying the cross-compilation settings. 4. Running qmake and make from the command line to cross-compile the application. 5. Copying the compiled binary to the Raspberry Pi 3 using scp to run it.

Uploaded by

ap8389546
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

QT Cross Compile How To

This document discusses cross-compiling Qt5 applications for the Raspberry Pi 3 using Buildroot. Key steps include: 1. Downloading and configuring Buildroot for the Raspberry Pi 3. 2. Adding Qt5 support to Buildroot and compiling it, which takes around two hours. 3. Creating a GUI application in Qt Creator and specifying the cross-compilation settings. 4. Running qmake and make from the command line to cross-compile the application. 5. Copying the compiled binary to the Raspberry Pi 3 using scp to run it.

Uploaded by

ap8389546
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Qt5 cross compile for raspberry pi 3

https://github.com/EliArad/MyDocuments

Pre-Requisites:
a. you need a raspberry pi board ( I used pi 3)
I used raspbian OS and not the outcome from buildroot , even though I used buildroot.
( next steps will be to use the buildroot image and replace raspbian OS)
b. Download and extract buildroot from https://buildroot.uclibc.org/
c. download the open source QT from https://info.qt.io/download-qt-for-application-development

in order to make it working I managed to do the following


using buildroot , I cross compile qt5

To allow QT5 we need to enable:


Depends on: BR2_INSTALL_LIBSTDCPP [=y] && BR2_USE_WCHAR [=y] &&
BR2_TOOLCHAIN_HAS_THREADS_NPTL [=y] && !BR2_STATIC_LIBS [=n]
&& !BR2_PACKAGE_QT [=n]

quick notes on buildroot ( I will create a different tutorial on what I know about buildroot)

I tried make those steps in Qt creator , but I did not succuess yes, because it did not found the
makefile.
So this tutorial ( which take me some time to understand all) will build the gui in QT Creator 5.8
but I will compile and copy the app to raspberry pi 3 using the command line.

I am using xconfig (the qt version gui) but sometimes uses the ncurses menuconfig
(dependencies will come up if they are missing, so just install them)

1. after downloading and extracting buildroot do:


make raspberrypi3_defconfig
it will copy the raspberrypi3_defconfig into .config in the local directory

2. select external tool chain


I will save time in compile the tools chain
once you have qt5 , feel free to add everything you need or dont :)
once we have all of that, we can type make.

You should wait for two hours and then see that you have the images ready.

There are many tutorials in the internet which I got helped.


Many of them saying to download the img of the raspberry linux and mount it for sysroot.

The idea is that we will use buildroot output and creation to cross compile QT.

The idea beyond qt cross compile is as follow:

if you go to output/host/usr where your top level build root is located you will be a directory called
mkspecs

every directory have two files:


qmake.conf qplatformdefs.h

each file define where the compile will be taken from and which compiler for any of the necessary
tools.
Gcc , g++, ar, and so on,

MAKEFILE_GENERATOR = UNIX
CONFIG += incremental
QMAKE_INCREMENTAL_STYLE = sublib

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)

# modifications to g++.conf
QMAKE_CC = arm-linux-gnueabi-gcc
QMAKE_CXX = arm-linux-gnueabi-g++
QMAKE_LINK = arm-linux-gnueabi-g++
QMAKE_LINK_SHLIB = arm-linux-gnueabi-g++

# modifications to linux.conf
QMAKE_AR = arm-linux-gnueabi-ar cqs
QMAKE_OBJCOPY = arm-linux-gnueabi-objcopy
QMAKE_NM = arm-linux-gnueabi-nm -P
QMAKE_STRIP = arm-linux-gnueabi-strip
load(qt_config)

The first change we are going to do is to add a new mkspec:

we are using the hf gnu compiler


so lets copy it from linux-arm-gnueabi-g++

cp -r linux-arm-gnueabi-g++/ linux-arm-gnueabihf-g++/

remember , it is a directory of two files , so we do cp -r.

now we need to edit the file and add the hf ( the hardware floating point compiler)

BTW , how do we know that we are using the EABIhf compiler?

From buildroot target architecture:

We only need to change the compiler to hf compile:

#
# qmake configuration for building with arm-linux-gnueabi-g++
#

MAKEFILE_GENERATOR = UNIX
CONFIG += incremental
QMAKE_INCREMENTAL_STYLE = sublib

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)

# modifications to g++.conf
QMAKE_CC = arm-linux-gnueabihf-gcc
QMAKE_CXX = arm-linux-gnueabihf-g++
QMAKE_LINK = arm-linux-gnueabihf-g++
QMAKE_LINK_SHLIB = arm-linux-gnueabihf-g++

# modifications to linux.conf
QMAKE_AR = arm-linux-gnueabihf-ar cqs
QMAKE_OBJCOPY = arm-linux-gnueabihf-objcopy
QMAKE_NM = arm-linux-gnueabihf-nm -P
QMAKE_STRIP = arm-linux-gnueabihf-strip
load(qt_config)
so I change every place where I have arm-linux-gnueabi-gcc to arm-linux-gnueabihf-gcc

Qt Creator

I did all the steps of creating device , declaring the compile and kit but it did not work

So for now , which is also good step is to document and show how to build the gui in QT and
compile in the command line.

Steps:
1. Download QT Creator 5.X ( I am using 5.8)
2. create a GUI

Create the directory and the name of the GUI project


Select QT embedded

from where it came?


I will show soon..
then next , next and finished.

When I am going to the project tab:


I can see the make: not found in the environment,
before we continue to the command line compilation , lets see how did I create the device:
( for now it does not help much , but later it will be solved: )

go to tools→options

in build and run, add a new compiler:

name it as you want , I called it arm-linux

in QT Versions , add a the qmake from buildroot:


Go to Kits..

Add new..

Add device type: ( that you need also to create)

the password for raspbian is pi and password is raspberry.


Enter name and ip

you can do a test to see that the device is responding


once your settings is
ok , you will not see
any red error:

Again , in this tutorial I choose to document my actions although I cannot compile directly from QT

open the edit mode and select the .ui file or just open the design mode
add some components.

Now lets go to compilation

open a terminal and move to the place where QT Creator creates your project

QtTest/HelloPi3Gui
I like to create a source script: name ACTIVATE.sh
1. add to the PATH the location of the build external toolchain,
incase you did not change the settings in buildroot, it will be located at output/host/usr/bin

export PATH=$PATH:/home/elia/Downloads/buildroot_for_raspi3/output/host/usr/bin
Create a compile script ( the killer line) or just run from the command line prompt:

/home/elia/Downloads/buildroot_for_raspi3/output/host/usr/bin/qmake HelloPi3Gui.pro -spec


linux-arm-gnueabihf-g++ CONFIG+=debug CONFIG+=qml_debug

lets explain:

we are using the qmake from buildroot - see location

the HelloPi3Gui.pro is a file that was created by QTCreator

Go to the QTCreator project and you can see this file:


the compile will run qmake to create a makefile for us ,
this is what qmake is doing , its create a complicated makefiles.

After we run our compile script , we can see that we have Makefile

And what we do when we have Makefile - we call to make.

If we will now run the make we will get many errors.

./../Downloads/buildroot_for_raspi3/output/host/usr/arm-buildroot-linux-
gnueabihf/sysroot/usr/include/qt5/QtCore/qbasicatomic.h:61:4: error: #error "Qt requires C++11
support" # error "Qt requires C++11 support"
here I did the following:

I edit ( using joe editor) the Makefile

and added c++ 11 support:

CXXFLAGS = -pipe --sysroot=/home/elia/Downloads/buildroot_for_raspi3/output/host/usr/arm-


buildroot-linux-gnueabihf/sysroot -g -Wall -W -D_REENTRANT -fPIC $(DEFINES) -std=c++11

for CXXFLAGS

And now the make ended without any errors


Do file HelloPi3Gui shows that this file is ARM. ( to see the file type and more info)

~/QtTest/HelloPi3Gui$ file HelloPi3Gui


HelloPi3Gui: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), dynamically
linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0,
BuildID[sha1]=7eba50bd1d4484bbc74dce9b3c7f16730de093ca, not stripped

That’s it, the file is ready to be copy to raspberry pi 3 using ssh:

sudo scp HelloPi3Gui [email protected]:/home/pi

if you want to copy without the password everytime:

sudo apt-get install sshpass

sudo sshpass -p "raspberry" scp HelloPi3Gui [email protected]:/home/pi

lets summery the steps we made:


1. download buildroot
2. make def config for raspberry pi 3
3. Add QT5, after adding the dependencies ( if not it will not shown in the menu)
4. compile buildroot – wait for two hours until it eneded.
5. install QT Creator 5.8
6. we did define the QT embedded kit , altgough we did not compiled from QT Creator it self
7. we run qmake ( the buildroot qmake ) with arguments of mkspec hf compiler and our .pro file
8. we got a Makefile, edit the Makefile to add C++ 11 flag support
9. we run make to produce the binary executable for our application.
10.copy using scp the binary to raspberry pi 3.
11. I run it there.

I want to use GUI and not console because I tried the similar steps with console app and QT4 ( the
default of buildroot)
there I were able to compile from the QT Creator and deploy to the target.
But when I tried to build a widget gui it failed to start.
I read some where the QT4 does not work so I moved to QT5 and it does run.
More notes:

because I am using raspbian OS I needed to install QT requirements on the target:


we have INTERNET and apt-get so we can install qt Platform Plugin Dependencies
http://doc.qt.io/qt-5/linux-requirements.html
and we can find many of them in the web.

Update:
1. we can create in the linux-arm-gnueabihf-g++/qmake.conf file
a flag for the compiler:
QMAKE_CXXFLAGS = -std=c++11

qmake will not create the Makefile with the C++11 compiler flag so we dont need to
edit the makefile

Upcoming documents:
* Top level of understanding build root , create img deploy and test
* Try fix the above issue and use QT Creator project full to compile and deploy to targeted
* do the same for beaglebone black
* Do the same for Yocto project.

You might also like