As described in our previousblogposts, 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.
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:
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:
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:
To watch this video on our website please or view it directly on YouTube
And voilà! VS Code now properly recognizes the include paths and navigates to Qt and GLib classes:
To watch this video on our website please or view it directly on YouTube
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.
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.
Miłosz Kosobucki
Senior Software Engineer
Miłosz Kosobucki is a Senior Software Engineer at KDAB
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.
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.