27 September 2015

[Second Edition] Step by Step: Developing an Application using CMAKE, QT and Boost.

Preface


I wrote the first edition of this tutorial for about 6 months ago.

Refer to the slide show here http://www.slideshare.net/DanielPfeifer1/cmake-48475415 by Daniel Pfeifer,
I found that I use a lot of anti-practice on the CMake script. So, I decided to write the second edition . The CMake script will be improved by
  • Avoid introducing new variables.
  • Avoid altering CMake’s global variables.
  • Avoid using non-target function such as INCLUDE_DIRECTORY, and LINK_DIRECTORY.
  • Use TARGET_COMPILE_FEATURES to enable C++11 instead of appending flag to the complier. ( CMake 3.x )
There are also other updates from the first edition.
  • Use newer version of libraries including QT 5.5.0, and Boost 1.59.
  • Update the command arguments of b2.
  • Use Kdevelop as an IDE on Ubuntu since it accepts CMakeList.txt by default.
  • Improve Syntax highlight. Remove custom html layout.

Introduction


This tutorial will show you how to create a CMake project that associated with Boost and QT library.  For better understanding, please download my completed project from

https://bitbucket.org/apivan/cmakeqtboostsample/downloads

That project uses Qwidget, Qml, boost signal2 and boost thread. It contains .UI and .RCC files. The app will be built on Windows and Ubuntu\Kubuntu Linux. The complier for Windows will be visual C++ 2013, and the complier for Ubuntu will be gcc-4.8.


Background


C++ is available on many platforms. So are the QT and Boost library. Unlike managed languages, C++ compiler generates the native binary file which cannot be run on other OS. Therefore, C++ source code has to be complied for certain compiler/OS in order to create a valid application. This is seemed to be an overhead, but the performance of native application is worth the trouble. See this article.


CMake


CMake is a free opensource tool for managing the build system. CMake will read your project infomation from CMakeList.txt and then, generate native build project file (ie. .sln , MakeFile). Incomplete lists of the tools that similar to CMake are make, Scons, and autotool.


QT


QT is a GUI and application framework. QT has their specific compiling steps. Fortunately, CMake already has a feature to handle that. Such features are AUTO_MOC, AUTO_UIC and AUTO_RCC. Incomplete lists of the tools that similar to QT are GTK, and Wxwidget.


Boost


A programing language without framework is powerless. The standard C++11 library is still small compare to other frameworks such as .NET. Thus, we need Boost library which contains almost everything you need for general work.

Installing CMake and QT 

Installing Boost

Source Files

Generating the Native Build Project

Developing and Debugging under IDE

Packing (Windows Only)


Disscusion

Creating cross-platfrom C++ project is not that difficult in these days. The setup may take time, but you will do this only once. Most of the code on CMakeList.txt is just if-else statement for choosing the right lib for compiler/OS. Adding new source file or referring to the module in external library is very easy. It is as simple as adding assambly reference in C#.