16 April 2015

Step by Step: Boost and QT on Mobile Phone


Download Source Code (Android)
Download Source Code (WinRt)

Introduction

This tutorial will show you how to create application with Boost and QT. Since the CMake does not officially support QT mobile, I decided to use QMake for this tutorial. I found that manually translate between CMake to QMake is easy. In addition, QMake file can be opened by QTCreator. This could resolve many dependency problem.

If you interested in using CMake,  [Taka-no-me, android-cmake] could let you do that.

This tutorial will only cover how to build the mobile application from windows host.

Background 


QMAKE

QMake is a build tool from QT. QMake will read .Pro and .Pri file for the build information. The .Pro and .Pri are also generated by QT creator.

Android NDK

At first, android can only be developed by Java language . Soon later, the android’s native development kid (NDK) was introduced [Android NDK]. This toolkit allows developer to develop the native application using C or C++. However, not all android features can be done in C++ such as accessing GPS [Access GPS Through Ndk, StackOverflow], and some android’s font-end interface modules. Thus, to create a practical app in C++, one must write a compatible layer for Java interoping. Fortunately, QT will handle this overhead for us [Android support, Qt io].

Android

First, download all of the required software including QT for android, Android SDK tools, Android NDK, Apache Ant and Java SE [Getting Started with Qt for Android, Qt io]

In this tutorial, I targeted only arm7 processor.

While installing QT, make sure that you already check the android package. See figure below.

Installing QT for android


For android SDK tools, you can simply download the “Android Studio for Windows”. Then install it from installer wizard. After done the installation, open the android SDK (Not the IDE) which, by default, located at {C:\Users\UserName\AppData\Local\Android\sdk}. Check the required packages and click install as shown in figure below.

Andorid SDK package manager

Installing Boost for android


For android, Boost has to be built by Android NDK. You can spacific boost.build from config file [Cross Complie, Boost build site][Cross-Compiling-Boost 1.56 for andorid ..., ruben torres bonet] has already, posted how to do this.

Follows this step to build an Boost for android arm7.

1. Extract the android NDK. In this tutorial, the android NDK will be extracted to {C:\}
2. Similar to the way to build Boost for PC application, go the Boost source folder and run boostrap.bat.
3. Modify the Boost.Build’s configuration file as follows

import option ;

using gcc : arm : arm-linux-androideabi-g++.exe ;
option.set keep-going : false ;


4. Open the command prompt. CD to boost folder and then add path to android NDK as follows

set path=%path%;C:\android-ndk-r10d\prebuilt\windows\bin;
set path=%path%;C:\android-ndk-r10d\toolchains\arm-linux-androideabi-4.9\prebuilt\windows\bin;


5. Run b2 with these arguments. As demonstrated in [Cross-Compiling-Boost 1.56 for andorid ..., ruben torres bonet], you can create a bat file and run it as follows.

b2 link=static install threading=multi threadapi=pthread target-os=android ^
    toolset=gcc-arm ^
    --without-python ^
    define=BOOST_MATH_DISABLE_FLOAT128 ^
    include=C:\android-ndk-r10d\sources\cxx-stl\gnu-libstdc++\4.9\include ^
    include=C:\android-ndk-r10d\sources\cxx-stl\gnu-libstdc++\4.9\libs\armeabi\include ^
    include=C:\android-ndk-r10d\platforms\android-19\arch-x86\usr\include ^
    --build-dir=C:\BoostAndroid\build ^
    --prefix=C:\BoostAndroid ^

As shown in my source code, the boost lib can be added to the .pro file. See code below.

INCLUDEPATH += C:/boostandroid/include/boost-1_57 android{ equals(ANDROID_TARGET_ARCH, armeabi-v7a){ LIBS += -LC:/boostandroid/lib \ -lboost_thread_pthread-gcc-mt-1_57 \ -lboost_system-gcc-mt-1_57 } equals(ANDROID_TARGET_ARCH, x86){ # Add x86 LIBS here } }













Creating an android emulator

Open the android AVD from {C:\Users\new\AppData\Local\Android\sdk}, then create an android arm 7 processor as shown in figure below. Before starting the emulator, ensure the option “Use host GPU” has been checked. Finally, start the emulator.

Configure android emulator



Specific tools for QT Creator

Open the Tool>Options and specific tools including NDK and Apache Ant. See fiture below.

Configuraion for android tool


Open the project page {Ctrl + 5}. Add an android-arm to the build target. See figure below.

Adding android build target


Now, build the project, if everything was setup correctly you could see the application running. Note that I got some error messages while opening the application. I cannot find any information about that message, but the application work just fine.





Boost and QT are running on android emulator


WinRt (Nokia lumia 630)

Currently, boost.future  is not supported in WinRt [How to build boostthread for winrt, msdn]. Also you can workaround using unofficial boost changeset forking [Using boost lib in windows store, msdn]
I have tried with other non-header boost module including boost::chrono It work very well. std::future also work on my windows mobile phone. Surprisingly, it look like std::future will run in std::launch::deffered mode by default.

Install development tools

The development tools for winrt are QT for winrt, ZUNE, and Visual studio express 2013 for desktop. Fortunately, Microsoft let you develop App on your windows phone at free of charge. Follows this [Register as an app developer], to register your development account.

Build boost for WinRT

Similar to android build, run b2 with following argurment.

b2 link=static install toolset=msvc-12.0 windows-api=store architecture=arm  --without-python --build-dir=C:BoostWinRtArm\build --prefix=C:\BoostWinRtArm

Specific tools for QT Creator

Unlike Android, WinRt support C++ as a first class development language. Thus, no extra language interloping is required. The way to  configure tools chain  for winRT is similar to QT's desktop app build. If you cannot find the debugger for winRT, you can still build and deploy on release mode.

QT and Boost on WinRt

 

Discussion

Mixing QML and QWidget by hosting QML on QWidget does not work correctly on both android and WinRt. It seem like the QWidget part won’t render the QML view [QQuickView: nothing rendered on Android, QT Bug report]. Even if the mixing work, it is still better to develop application on QML. I feel that the interface from QWidget doesn’t fit the mobile phone as you can notice on the figures below.

QWidget on Android Emulator
QWidget on WinRt phone