Speeding up compilation on Ubuntu with Qt Creator

Are you reading random stuff on the web while waiting for your C++ compilation to finish? Then you have come to the right place. In this post I will tell you about two really nice tweaks you may do to speed up your compilations, namely ccache and the make -j flag, and how you may set these up in Qt Creator.

ccache

ccache is a clever tool that wraps you compiler (g++ or mpicxx) and understands whether the file you are compiling has been compiled before with exactly the same contents and settings. If it has, it just returns a cached result.

Unlike regular make, ccache is extremely good at detecting the true state of what you are compiling. I have never had any trouble with ccache.

This really speeds up the compilation when you are using make clean, especially if you are switching git branches. In other words, it is a much simpler solution to achieve fast compilation with git branches than to create separate build folders for each branch.

To enable ccache, install it with

sudo apt-get install ccache

and add the following to your .pro file:

QMAKE_CXX = ccache g++

Replace g++ with mpicxx if you are using MPI.

The make -j flag

I realized when compiling the Qt source that make has a -j flag that enables threaded compilation on all available processors on the machine. This also speeds up compilation significantly, and I made a 3.55x performance gain on a 4 core CPU. To enable this flag, go to the Projects view in Qt Creator and add the following arguments to the make build step:

-j

This should look something like this afterwards:

This is how your project settings should look like after adding the -j option to make.
This is how your project settings should look like after adding the -j option to make.

If you prefer not to use all available processors for compilation, you may add a number after -j to set the number of processors. For instance make -j 3 would compile with 3 processors.

Published by

Svenn-Arne Dragly

I'm a physicist and programmer, writing about the stuff I figure out as I go.

One thought on “Speeding up compilation on Ubuntu with Qt Creator”

  1. Hi

    I know this is an oldish post but would like to give another tip: use ninja build instead of make!
    On our project, make takes well over 20 seconds to decide there’s nothing to build! ninja is instantaneous. Doesn’t make a huge difference on complete builds but it has changed our life for incremental builds!

    If you’re using CMake, switching to ninja is a simple matter of specifying the correct generator. Not sure how you do that with qmake…

Leave a Reply