-
Notifications
You must be signed in to change notification settings - Fork 1
Debugging
In the following, we describe two different ways how we can debug our code directly in VS Code:
- Using the CodeLLDB extension together with the lldb debugger
- Using CMake Tools + Microsoft's C/C++ extension
The author suggests to use the latter because of its simplicity.
Before using a debugger, it is probably a good idea to compile our code in debug mode. If you haven't done so,
- Run
>CMake: Select a Variantand selectDebug - Run
>Cmake: Build
This creates a (new) build directory (build.Clang-21.1.2-Debug in our case).
We want to debug ./examples/ex2.cpp.
To set a breakpoint,
- Open the source file
./examples/ex2.cpp - Click next to the line number 9
The red dot indicates that a breakpoint has been set on this specific line.
In contrast to Microsoft's C/C++ extension, clangd does not come with a debugging feature.
Here we show how to use the CodeLLDB extension together with the lldb debugger.
To install/enable the extension, follow the same steps as in the previous sections.
To use the debugger in VS Code, we have to provide a launch.json file.
Fortunately, CodeLLDB makes it fairly easy to generate a basic template:
- Click on
Run and Debugin the activity bar (triangle with the bug) - Choose
create a launch.json file - Select
CodeLLDB
This creates a launch.json in our project settings folder .vscode:
Apply the following changes:
- Click on
Run and Debugin the activity bar - Click the green play icon next to
Debug ex2 - Start debugging
We can now examine variables, step into functions, etc.
It is also possible to use the usual commands in the Debug Console:
If we want to debug a different executable or run the same executable with different input arguments, we can simply add a new configuration to the launch.json file:
Then we select the appropriate configuration from the drop down menu next to the green play icon in the Run and Debug window:
Combining CMake Tools with the debugging features from Microsoft's C/C++ extension makes it super easy to start a debugging session.
However, we want to note that other approaches, e.g. using a launch.json file, can be more flexible and powerful.
To install/enable the extension, follow the same steps as in the previous sections.

In the bottom right corner in the screenshot above, VS Code already warns us that we have both clangd and Microsoft's C/C++ extension installed. Their language server features don't work well together.
Since we only want the debugging features from the Microsoft's C/C++ extension, we will disable its Intellisense as suggested by VS Code.
We can do this by either clicking Disable Intellisense in the pop-up window or by updating the user settings JSON file:
- Run
>CMake: Select Launch/Debug Target - Select
ex2 - Run
>CMake: Debug - Start debugging
We can now examine variables, step into functions, etc.
It is also possible to use the usual commands in the Debug Console:
To change executable, we simply run >CMake: Select Launch/Debug Target again and select a different target.