Cristian Adam

Bundling together static libraries with CMake

In this article I’m going to talk about building a C++ library with CMake, but it won’t be a CMake tutorial.

Let’s say you have a C++ library which depends upon a few open source libraries, which have a CMake project structure, but not necessarily done by the book (which means that they get only get built, and not deployed / installed)

Your library will include tests (unit-tests / integration tests), and the deployment can be just packing the headers and the binaries together in a tar.gz file.

This is not necessarily by the book, but it will do the job, and it could fit into any build system that the client has.

A book that one can use to do CMake right is Profesional CMake. Awesome CMake also has a great list of resources regarding CMake.

Coming back to the C++ library, which decisions do we take to build it? Shared library, static library, both?

Speeding up libclang on Windows

In this article I am revisting an article from three years ago: “Speeding up libclang on Windows”, in which I was having a look at how the experimental Clang Code Model was handling a particular source code file.

With the help of Profile Guided Optimization I was able to go down from 10 seconds to 6 seconds.

In the meantime the Clang Code Model has been enabled by default in Qt Creator 4.7.

Three years ago I tested Qt Creator 3.6.0, Qt 5.5.1, LLVM/Clang 3.6.2, MinGW GCC 5.3.0, Visual C++ 2013/5. I tested on a Lenovo W510 Thinkpad with an “Intel(R) Core (TM) i7 CPU M 620 @ 2.67 GHz” CPU.

Now I am going to test Qt Creator 4.8.2, Qt 5.12.2, LLVM/Clang 7.0.1, MinGW GCC 7.3.0, and Visual C++ 2017. I upgraded my laptop to a Lenovo A485 Thinkpad with an “AMD Ryzen 7 Pro 2700U w/ Radeon Vega Mobile Gfx 2.20 GHz” CPU.

How many seconds would it take libclang to parse the file? TL;DR? 3 seconds!

Modifying the default CMake build types

CMake has for single configuration configurators the following build types (configuration):

  • Empty (Qt Creator wrongly refers to this as “Default”)
  • Debug
  • Release
  • RelWithDebInfo – Release with debug information, needed for profiling / post mortem debugging
  • MinSizeRel – Release optimized for size, and not for speed.

If we have a look at CMake’s Modules/Compiler/GNU.cmake we can see:

  # Initial configuration flags.
  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
  string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " -g")
  string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG")
  string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -O3 -DNDEBUG")
  string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -O2 -g -DNDEBUG")

The empty build type usually contains the common build flags for all build types. It is generated from the CMAKE_C_FLAGS_INIT / CMAKE_CXX_FLAGS_INIT variables, and the CFLAGS / CXXFLAGS system environment variables.

But in the case of an IDE like Qt Creator makes no sense to have, you will end up for GCC with a -O0 (Debug) build. I’ve opened QTCREATORBUG-22013 in this regard.

CMake uses the CMAKE_<LANG>_FLAGS_<CONFIG>_INIT variables which will be used to populate the CMAKE_CMAKE_<LANG>_FLAGS_<CONFIG> variables.

There are cases when you might want to change the default build types:

  • Want to have -g1 for RelWithDebInfo, because your binaries are becoming too big
  • Want to improve build times in Debug mode with -gsplit-dwarf
  • Want to link to a different version of the CRT
  • Want to enable all possible warnings from the compiler

Lastly, we want to do all this without putting if clauses in the code, and manually changing the CMAKE_<LANG>_FLAGS variables. The rule of thumb is: if you have to change compiler flags, you should do it in a toolchain file!

A Better QNX CMake Toolchain File

At the end of October 2018 on the Qt development mailing list it was announced that CMake was chosen as the build system (generator) for building Qt6. That also meant that The Qt Company will gradually stop investing in their in house Qbs build system.

I personally think is a good idea to have major C++ projects like Boost (July 2017 switch announcement! ), LLVM/Clang, and now Qt to use CMake as their build system (generator). We C++ developers should work together in having a common build system.

There was a bit of email traffic on this topic. There was some skepticism of CMake being able to support specialized operating systems like QNX, so I pointed to an October 2017 blog entry of Doug Schaefer named QNX CMake Toolchain File. There Doug Schaefer presents us with a minimal CMake Toolchain File.

Since I am lucky(sweat_smile) to have a QNX 7.0 license I tried to compile and run the recently released CMake 3.13.0 for the QNX 7.0 x86_64 target!

Speeding up CMake

At the beginning of this year Bits’n’Bites wrote an article named Faster C++ builds, in which it’s being described how you can accelerate building LLVM using ninja, using a cache etc.

The following excerpt caught my eye:

For most developers, the time it takes to run CMake is not really an issue since you do it very seldom. However, you should be aware that for CI build slaves in particular, CMake can be a real bottleneck.

For instance, when doing a clean re-build of LLVM with a warm CCache, CMake takes roughly 50% of the total build time!

So I decided to build LLVM 4.0.0 (and clang) on my 2011 Core i7 Lenovo W510 laptop and see if I can reproduce his findings.

Qt Creator, Ubuntu, and VirtualBox

It is common for IT companies (at least in Germany, automotive field) to use Ubuntu Linux LTS in a VirtualBox on Windows or Mac hosts. This way the employee can use Microsoft Outlook / Office, Microsoft Skype, Cisco Spark, or other proprietary collaboration tools, and at the same time use the supplied virtual machine for development.

By default VirtualBox doesn’t configure any 3D acceleration or multi-core CPU for the guest. One needs to change these settings in order to have a more responsive desktop environment and to compile faster smile Also important not to forget about the installation of the VirtualBox Guest Additions.

Running glxinfo on a Ubuntu Linux 16.04 LTS in VirtualBox 5.1.18 gives back this information:

OpenGL vendor string: Humper
OpenGL renderer string: Chromium
OpenGL version string: 2.1 Chromium 1.9
OpenGL shading language version string: 3.30 NVIDIA via Cg compiler

As it turns out this is not enough to run Qt Creator 4.2.1. Qt Creator simply displays a black welcome screen on Ubuntu Linux 16.04 LTS, or simply crash on Ubuntu 14.04 / 12.04 LTS:

If Qt Creator is run from command line, it will give out these messages (Ubuntu 16.04 LTS):

QOpenGLFramebufferObject: Unsupported framebuffer format.
QOpenGLFramebufferObject: Unsupported framebuffer format.

If you do a web search after “Qt Creator VirtualBox crash” you will find out how to fix this problem – either disabling the welcome plug-in, or disable the 3D acceleration of your VirtualBox.

Disabling the 3D acceleration means that the system will use a software OpenGL driver.

But then again why not simply use a software OpenGL driver just for Qt Creator and not for the whole system?

NullPointerException in C++

For those familiar with languages like Java, and C#, something like NullPointerException shouldn’t come as a surprise. But what about C++? C++ also has exceptions, right?

In C++ reading or writing at address zero is an access violation. By default an access violation will result in the immediate termination of the program. What else results in immediate termination of the program? Division by zero! There is no ArithmeticException, only a swift termination!

The OS’ SDK usually provides a way to catch such access violations and recover from them. This way of catching access violations involves a C callback method and a bit of setup.

Wouldn’t be nice if the setup would be one line of code and the C callback function would throw C++ exceptions behind the scenes?

But it does work like this. At least on Windows and Linux (I don’t have access to a macOS machine), and only with a few select compilers.

Before going further into details I would like to present my test case: define functions which do:

  • Division by zero
  • Reading from nullptr
  • Writing at nullptr
  • Write to an empy vector with the subscript operator []
  • Read from an uninitialized shared_ptr

Execute them ten times to make sure that this is not only one time “wonder”. Every try block will have an instance of a RAII Message object to make sure that stack unwinding is taking place, and that we won’t have any resource leaks.

C++ I/O Benchmark

In this post I will talk about copying files. I will read one file in chunks of 1MB and write it to another file.

C++ provides three cross platform APIs for I/O (input/output):

  1. C FILE API (fopen, fread, fwrite)
  2. C++ API (std::ifstream, std::ofstream)
  3. POSIX API (open, read, write)

The POSIX API requires a bit of #ifdef-ing to get it working cross platform, but it’s not that scary.

Reading and writing 1 MB of data should work more or less as fast for all APIs, right?

I have run the benchmark on my SSD powered Lenovo Core i7 laptop running Windows 10 and Kubuntu 15.10, and on a SSD powered Raspberry PI2 running the latest Raspbian.

The code for the benchmark is below:

Introducing C++ experimental io2d

In this post I will be talking about P0267R0: A Proposal to Add 2D Graphics Rendering and Display to C++. This proposal will be discussed next week at the next ISO C++ Standardization meeting in Jacksonville.

P0267R0 comes out of C++’s SG13 HMI: Development of new proposals in selected human-machine interaction such as low-level graphics/pointing I/O primitives.

SG13 was created by Herb Sutter after the One C++ keynote talk he gave at GoingNative 2013.

Speeding up libclang on Windows

In this article I will tackle libclang’s speed on Windows, in particular Qt Creator’s clang code model.

Qt Creator 3.6.0 fixed the following bug: QTCREATORBUG-15365: Clang Model: code completion speed regression. The bug report contains information on how to enable Qt Creator’s clang code model statistics. This is done by setting this environment variable: QT_LOGGING_RULES=qtc.clangbackend.timers=true.

On Windows Qt Creator will output this information in Windows debugger output. I use DebugView to view this information.

libclang is used by Qt Creator to provide code completion support. The clang code model is still experimental and not 100% feature equivalent with the Qt Creator built-in code model.

By using the clang code model it means that Qt Creator uses a real C++ compiler to parse the source code you are editing. It also means that if you are having a big source file, with lots of includes, it will take some time to do so.

Qt Creator will cache this information in a form of a pch file under %temp%/qtc-clang-[some letters]/preamble-[some numbers].pch file. The complete compilation is done only once. The subsequent code completion commands are fast.

I have picked Lyx – The Document Processor as a test project for Qt Creator. Lyx uses Boost and Qt5 and on my Intel(R) Core (TM) i7 CPU M 620 @ 2.67 GHz Windows 10 powered laptop it takes, for Text3.cpp, approximately 10 seconds to “compile”.

Even though my laptop has multiple cores, libclang will use only one core to compile Text3.cpp. What can we do about it? It would be nice if libclang could use the GPU smile

Qt Creator 3.6.0 ships with libclang 3.6.2, and for Windows it ships a Visual C++ 2013 32 bit build, unlike Linux where 64 bit is the norm.

I will take clang 3.6.2 and compile it Visual C++ 2013, Visual C++ 2015, Clang 3.7.0 and Mingw-w64 GCC 5.3.0. I have managed to get libclang to compile Text3.cpp in approximatively 6 seconds. Which C++ compiler was able to this?