Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
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.
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:
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.
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
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
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.
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
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications. In addition to being leading experts in Qt, C++ and 3D technologies for over two decades, KDAB provides deep expertise across the stack, including Linux, Rust and modern UI frameworks. With 100+ employees from 20 countries and offices in Sweden, Germany, USA, France and UK, we serve clients around the world.
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Learn Modern C++
Our hands-on Modern C++ training courses are designed to quickly familiarize newcomers with the language. They also update professional C++ developers on the latest changes in the language and standard library introduced in recent C++ editions.
Learn more
2 Comments
10 - Aug - 2021
J. Holden
Great tutorial, very well detailed, thanks!
11 - Aug - 2021
Miłosz Kosobucki
Thanks! Glad that you liked it.