Skip to content

Improving C++ Development in Visual Studio Code with compile_commands.json and Bear

As described in our previous blog posts, you can use Visual Studio Code as an IDE for C++ projects. It works particularly well with CMake-based projects. Unfortunately, with build systems not supported natively by VS Code, you need to manually configure include paths and compilation definitions, for the code model to work properly.

Since the October 2017 update, the VS Code C++ extension supports Compilation Database in the compile_commands.json file.

If you use this file, VS Code IntelliSense engine will automatically pick up the correct include paths and defines. I’ll show you how to configure it and which benefits it provides.

Problem

Imagine that, for some reason, you need to work with a C/C++ project that isn’t managed with CMake. In our example, I’ll use qmake, an older build system from the Qt Framework. Let’s also say that your project uses system libraries discovered by pkg-config, for example GLib.

Once you open such project in VS Code, the code model will not recognize Qt and GLib’s include paths: include errors for Qt and GLib headers

You won’t be able to Ctrl+click to follow symbols; you’ll get error squiggles under each unrecognized #include, an overall annoying experience.

Of course, you can go to c_cpp_properties.json and painstakingly configure your "includePath" and "defines," but that’s a lot of work — particularly if your project is large and uses multiple external libraries.

Solution: The Bear Enters ʕ·ᴥ·ʔ

Bear is a tool that can monitor the execution of arbitrary build tools (like make or ninja) and generate the compile_commands.json file for you. Later, you’ll point VS Code towards this file and the code model will pick up the necessary configuration.

I’ll show how to do it for a qmake project that uses GLib through pkg-config. Let’s assume that the project is located in ~/Documents/projects/TestQtApp and you want to build it in ~/Documents/projects/build-TestQtApp. The project .pro file looks as follows:

QT       += core gui widgets

CONFIG += c++11 link_pkgconfig
PKGCONFIG += glib-2.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

Step 1

Generate Makefiles from the project’s .pro file:

$ cd ~/Documents/projects/build-TestQtApp
$ ~/Qt/5.15.2/gcc_64/bin/qmake ../TestQtApp
Info: creating stash file /home/milosz/Documents/projects/build-TestQtApp/.qmake.stash

Step 2

Now we’ll summon Bear, to help us create the Compilation Database:

$ sudo apt install bear
$ bear -- make

After this, your build directory will contain the compile_commands.json file that will contain the compilation entries for each of your project files:

{
"arguments": [
"/usr/bin/x86_64-linux-gnu-g++-10",
"-c",
"-pipe",
"-O2",
"-std=gnu++11",
"-Wall",
"-Wextra",
"-D_REENTRANT",
"-fPIC",
"-DQT_NO_DEBUG",
"-DQT_WIDGETS_LIB",
"-DQT_GUI_LIB",
"-DQT_CORE_LIB",
"-I../TestQtApp",
"-I.",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-I../../../Qt/5.15.2/gcc_64/include",
"-I../../../Qt/5.15.2/gcc_64/include/QtWidgets",
"-I../../../Qt/5.15.2/gcc_64/include/QtGui",
"-I../../../Qt/5.15.2/gcc_64/include/QtCore",
"-I.",
"-I/usr/include/libdrm",
"-I.",
"-I../../../Qt/5.15.2/gcc_64/mkspecs/linux-g++",
"-o",
"main.o",
"../TestQtApp/main.cpp"
],
"directory": "/home/milosz/Documents/projects/build-TestQtApp",
"file": "/home/milosz/Documents/projects/build-TestQtApp/../TestQtApp/main.cpp",
"output": "/home/milosz/Documents/projects/build-TestQtApp/main.o"
}

As you can see, all the required Qt and GLib include paths are there.

Step 3

Now, let’s point VS Code to our shiny new database. You need to go to the workspace C/C++ extension settings and post the full path to the file in the Advanced section:

And voilà! VS Code now properly recognizes the include paths and navigates to Qt and GLib classes:

About KDAB

If you like this article and want to read similar material, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

2 thoughts on “Improving C++ Development in Visual Studio Code with compile_commands.json and Bear”

Leave a Reply

Your email address will not be published. Required fields are marked *