Cross-Compiler setup for Raspberry Pi and Qt5

This is my second blog to help people to cross compile Qt applications on Ubuntu machine for Raspberry Pi 2/3.

What you need

  1. Ubuntu machine, this blog assumes you have 64-bit Ubuntu machine.
  2. Working internet connection

Setup

  1. Open terminal (Ctrl + Alt + t)
  2. Paste the following
    • mkdir ~/opt
      cd ~/opt
  3. 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
  4. Mount Rootfs (Option 1)
       Option 1 :Mount the root filesystem of Raspbian image    

    1. Download the latest image from here
      wget http://downloads.raspberrypi.org/raspbian_latest -O wheezy-raspbian-latest.zip
      unzip wheezy-raspbian-latest.zip
    2. mount it on some mount point ( say, /mnt/rasp-pi-rootfs)
      sudo mkdir /mnt/rasp-pi-rootfs
      sudo mount -o loop,offset=62914560 2015-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 commandsScreenshot from 2016-07-12 23:56:24
      • fdisk -lu /path/to/img/file
      • Multiply the start sector (in this case 122880) by 512 i.e. 122880*512=62914560
    3. and your rootfs will e mounted
  5. Mount Rootfs (Option 2)
       Option 2 : Mount the root filesystem of existing working Raspberry Pi using NFS

    1. 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.
    2. 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
    3. The above command will open Raspberry Pi terminal
    4. 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

    5. Create a directory on Ubuntu machine to mount rootfs ( say, /mnt/rasp-pi-rootfs)
      sudo mkdir /mnt/rasp-pi-rootfs
    6. 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
    7. If mounted properly you shoud see the rootfs of Raspberry Pi on Ubuntu Machine in the directory /mnt/rasp-pi-rootfs
  6. 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
  7. For 64-bit Ubuntu users
     sudo apt-get install ia32-libs
  8. Clone the cross-compile-tools repository

     git clone https://git.milosolutions.com/other/cross-compile-tools.git
  9. 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
  10. 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 install

    Note: use the appropriate one

    • linux-rasp-pi-g++
      OR
    • linux-rasp-pi2-g++
  11. 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…!! 

  12. 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

Happy Qt’ing’ Option 1 users…!! 

3 thoughts on “Cross-Compiler setup for Raspberry Pi and Qt5

  1. Pingback: Cross-Compiler setup for Raspberry Pi and Qt5 | ashwindeshpande

Leave a comment