Organizing build commands in cmake with profiles

When dealing with a C/C++ project that utilizes cmake one will sooner or later need to organize the available build options into profiles than enable multiple features in a convenient manners. Two of the ways to achieve that is the use of cmake cache files and cmake presets.

Let’s start with setting a common ground for this test using the vtk repository:

git clone https://gitlab.kitware.com/vtk/vtk
cd vtk/
git switch -d v9.1.0
git submodule init
git submodule update --recursive

VTK has already a number of documented build options on top of the ones that cmake provides for every project.

[Read More]

Generating compile commands with cmake

cmake offers the option to generate a compilation database in json format using the option CMAKE_EXPORT_COMPILE_COMMANDS=YES. The database holds essentially the command, paths and compilers flags used to compile each file of a project. The generated file (usually called compile_commands.json) can be used in conjunction with various code completion tools like YouCompleteMe and Visual Studio’s IntelliSense in order to offer more complete information, completion and errors about the code.

The official cmake tutorial

There are many resources, posts and articles about using cmake and writing modern cmake code. Unfortunately for a new user it is sometimes difficult to locate the proper resource that contains no deprecated options or any other non-recommended practices. One of the quickest and simplest resource I became aware recently is the official tutorial that lives in the documentation of cmake.

Using ccache to speedup compilation of C/C++

ccache is a utility that caches intermediate files during compilation offering significant speedups. It is especially useful if you are switching between two branches of the same project see below.

To use it, prepend it before the compiler i.e. Use ccache gcc instead of gcc ... . If you are using cmake with a version greater that 3.4 you can use the option -DCMAKE_CXX_COMPILER_LAUNCHER=ccache for older version you can modify the compiler settings or use the variable RULE_LAUNCH_COMPILE for more see here.

[Read More]