Visual Studio Code as a C++ Development Environment
Overview
With enough time spent on setup and configuration, VSCode can work well as an IDE for C++ projects. VSCode's flexible
configuration model makes is straightfoward to work with.
These are some notes on one way of getting set up.
Baseline
Assuming the official
C/C++ VSCode extension
is installed, we first need to set the default include path in the global
settings.json
. If there are libraries on disk used frequently enough to warrant
being visible to all projects (whether they're linked or not), this is the place to list them:
"C_Cpp.default.includePath": ["path-one", "path-two"]
For every C++ project worked on, we'll also want to create a workspace
.vscode/c_cpp_properties.json
to store project specific configuration details such
as the intellisenseMode
, includePath
,
compilerPath
and cppStandard
.
Each project's c_cpp_properties.json
should include the default
includePath
defined in settings.json
. This way, the
default path will be searched in addition to any project specific paths. Also ensure that the
c_cpp_properties.json
configuration aligns perfectly with whatever tooling is being
used for the project. The intellisenseMode
needs to be spot on, else VSCode may
give misdirected warnings about missing headers.
Intellisense
It's important to keep in mind that configuration defined in c_cpp_properties.json
only
impacts intellisense for the project in which it is included. By setting a default C/C++
includePath
in a workspace or global settings.json
,
and setting an includePath
in a project specific
c_cpp_properties.json
that points to this default, all we are doing is making
VSCode's intellisense aware of the existence of these default headers for that specific project.
If the includePath
entries in these configuration files were omitted, VSCode would
have no way to enforce intellisense in any meaningful sense. In this case, warnings about missing symbols might get
thrown, but projects may still compile. This is because most compilers perform their own predefined searches for headers
during compilation.
Ultimately, the process of setting a sensible includePath
for C++ projects is to align
VSCode's intellisense header search as closely as possible with the underlying compiler search, so that symbol resolution
reflects what the compiler will be able to find.
Similarly, to get the compiler to find external libraries when linking, set up a
.vscode/tasks.json
with an appropriately matching
includePath
entry, or do away with task based header resolution and use a dedicated
build system such as CMake.
Integrating external tooling
VSCode allows users to integrate external tooling common to C++ development exactly as it does with any other language.
A means to call compilers, linters and formatters is provided through the
tasks.json
configuration file. Here you can define commands that run anything
you otherwise would from a terminal, all without leaving the editor window.
For a rundown of what can be done using tasks in VSCode, the
documentation gives a detailed
overview.