This is my second blog to help people to cross compile Qt applications on Ubuntu machine for Raspberry Pi 2/3.
What you need
- Ubuntu machine, this blog assumes you have 64-bit Ubuntu machine.
- Working internet connection
Setup
- Open terminal (Ctrl + Alt + t)
- Paste the following
-
mkdir ~/opt cd ~/opt
-
- From here you have 2 options,
- Option 1 : Mount the root filesystem of Raspbian image
OR - Option 2 : Mount the root filesystem of existing working Raspberry Pi using NFS
- Option 1 : Mount the root filesystem of Raspbian image
- Mount Rootfs (Option 1)
Option 1 :Mount the root filesystem of Raspbian image- Download the latest image from here
wget http://downloads.raspberrypi.org/raspbian_latest -O wheezy-raspbian-latest.zip unzip wheezy-raspbian-latest.zip
- mount it on some mount point ( say, /mnt/rasp-pi-rootfs)
sudo mkdir /mnt/rasp-pi-rootfs
sudo mount -o loop,offset=
629145602015-05-05-raspbian-wheezy.img/mnt/rasp-pi-rootfs- The value is not always 62914560, it depends on the image you have downloaded, to know the offset value use following commands
fdisk -lu /path/to/img/file
- Multiply the start sector (in this case 122880) by 512 i.e. 122880*512=62914560
- and your rootfs will e mounted
- Download the latest image from here
- Mount Rootfs (Option 2)
Option 2 : Mount the root filesystem of existing working Raspberry Pi using NFS- You need to setup NFS on Raspberry Pi (via Ethernet or Wi-Fi), ensure Ubuntu Machine and Raspberry Pi are on same Wi-Fi network or connected via LAN Cable.
- Open Raspberry Pi terminal on Ubuntu via ssh, to access Raspberry Pi use following command with proper place holders
sudo ssh pi@IP_ADDRESS_OF_Pi
- The above command will open Raspberry Pi terminal
- Now we need to install the nfs-kernel-server package first and configure it*.
sudo apt-get install nfs-kernel-server sudo vi /etc/exports
Add the following line to the /etc/exports file and save it:
/ *(rw,sync,no_subtree_check,no_root_squash,insecure)
the NFS server will fail to start because rpcbind isn’t running, to fix it
sudo -i cd /etc/rc2.d ln -s ../init.d/rpcbind S01rpcbind update-rc.d rpcbind defaults exit
* source here
- Create a directory on Ubuntu machine to mount rootfs ( say, /mnt/rasp-pi-rootfs)
sudo mkdir /mnt/rasp-pi-rootfs
- Now mount the Raspberry Pi’s root filesystem to the mount point on the host we created earlier. So, on the Ubuntu, run:
sudo mount IP_ADDRESS_OF_Pi:/ /mnt/rpi-rootfs
- If mounted properly you shoud see the rootfs of Raspberry Pi on Ubuntu Machine in the directory /mnt/rasp-pi-rootfs
- Download the cross compiling toolchain
wget https://www.dropbox.com/s/sl919ly0q79m1e6/gcc-4.7-linaro-rpi-gnueabihf.tbz
tar -xf gcc-4.7-linaro-rpi-gnueabihf.tbz
- For 64-bit Ubuntu users
sudo apt-get install ia32-libs
- Clone the cross-compile-tools repository
git clone https://git.milosolutions.com/other/cross-compile-tools.git
- Clone and init the Qt5 repository (please be patient after running the following command)
git clone git://code.qt.io/qt/qt5.git cd qt5 ./init-repository
If the above command fails for some reason then while retrying use:
./init-repository -f
- Compiling qtbaseNow 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 (please be patient may take few hours to complete):
./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 installNote: use the appropriate one
- linux-rasp-pi-g++
OR - linux-rasp-pi2-g++
- linux-rasp-pi-g++
- Compiling other modulesNow 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:
ashwin@ashwin:~/opt/qt5$ cd qtimageformats ashwin@ashwin:~/opt/qt5/qtimageformats$ /usr/local/qt5pi/bin/qmake . ashwin@ashwin:~/opt/qt5/qtimageformats$ make -j4 ashwin@ashwin:~/opt/qt5/qtimageformats$ sudo make install
NOTE: For those who have mounted the rootfs with NFS (using Option2), they are ready to go..!!
Happy Qt’ing’ Option 2 users…!! - Option 1 users: Transferring the changed image back onto SD Card
ashwin@ashwin:~/opt/qt5$ cd~/opt/ ashwin@ashwin:~/opt$ sync; sudo umount /mnt/rasp-pi-rootfs ashwin@ashwin:~/opt$ sudo dd bs=1M if=/path/to/.img/ of=/dev/sdb; sync
Note: use the appropriate one (DONOT forget to use
sync
)- sdb or sdc
Pingback: Cross-Compiler setup for Raspberry Pi and Qt5 | ashwindeshpande
Thank you for sharing process ,it will realy helpful ..
LikeLike
interesting which cards avaible for cross-compile-tools ?
LikeLike