Skip to content

Using CMake with Clang

If you write C++, using the clang suite for compilation, code completion, and other bells and whistles is a great way to improve your coding efficiency (especially if your a Vim user). This guide walks through many of the steps required to build your CMake projects with clang and correctly integrate the clangd language server with your preferred editor.

Install the Compilers

First check whether the clang suite of compilers and programs are already installed on your system.

sh
$ clang++ --version
Ubuntu clang version 12.0.0-3ubuntu1~20.04.5
$ clang --version
Ubuntu clang version 12.0.0-3ubuntu1~20.04.5
$ clangd --version
Ubuntu clangd version 12.0.0-3ubuntu1~20.04.5
$ clang-format --version
Ubuntu clang-format version 12.0.0-3ubuntu1~20.04.5
$ clang++ --version
Ubuntu clang version 12.0.0-3ubuntu1~20.04.5
$ clang --version
Ubuntu clang version 12.0.0-3ubuntu1~20.04.5
$ clangd --version
Ubuntu clangd version 12.0.0-3ubuntu1~20.04.5
$ clang-format --version
Ubuntu clang-format version 12.0.0-3ubuntu1~20.04.5

If they are not, then install them.

sh
sudo apt install clang-12 clangd-12 clang-format-12 clang-tools-12
sudo apt install clang-12 clangd-12 clang-format-12 clang-tools-12

Likely, this won't install the clang, clang++, clangd, and clang-format executables. Instead clang-12, clang++-12, etc. will be installed (almost certainly) into the /usr/bin folder. Verify this with which clang-12, which clang++-12, etc.

Best practice is to create symlinks to clang and clang++ using update-alternatives (see [[../../linux/Using Update Alternatives]]).

sh
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-12 12
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-12 12
sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-12 12
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-12 12
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-12 12
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-12 12
sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-12 12
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-12 12

Use clang --version as above to verify that you're using version 12.

Building

Option 1: CLI Flags

When generating your project's Makefile, you can pass certain flags to cmake to set your C and C++ and create a file, compile_commands.json, used by clangd to enable go-to definitions and code completion.

sh
mkdir build && cd build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
      -DCMAKE_C_COMPILER=/usr/bin/clang \
      -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
      ..
make
mkdir build && cd build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
      -DCMAKE_C_COMPILER=/usr/bin/clang \
      -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
      ..
make

Option 2: Environment Variables

The CC and CXX environment variables can be set to ensure cmake uses specific C and C++ compilers. As a one-off, you can set these enviornment variables before calling cmake.

sh
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++

However, to make persist these variables it is best export them in your .bashrc (or .zshrc or other).

sh
mkdir build && cd build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
make
mkdir build && cd build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
make