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.