Skip to content

Debugging

Thomas Hahn edited this page Nov 1, 2025 · 3 revisions

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.

Compile with debug flags

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 Variant and select Debug
  • Run >Cmake: Build

This creates a (new) build directory (build.Clang-21.1.2-Debug in our case).

Set a breakpoint

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
codelldb_breakpoint

The red dot indicates that a breakpoint has been set on this specific line.

CodeLLDB

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.

Install the extension

To install/enable the extension, follow the same steps as in the previous sections.

codelldb_extension

Create a template launch.json

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 Debug in 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:

codelldb_launch_json0

Apply the following changes:

codelldb_launch_json1

Run the debugger

  • Click on Run and Debug in 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:

codelldb_debug

Add another configuration

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:

codelldb_launch_json2

Then we select the appropriate configuration from the drop down menu next to the green play icon in the Run and Debug window:

codelldb_debug_ex3

CMake Tools + Microsoft's C/C++ extension

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.

Install the extension

To install/enable the extension, follow the same steps as in the previous sections. cpp_extension

Settings

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:

cpp_settings

Run the debugger

  • 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:

cpp_debug

To change executable, we simply run >CMake: Select Launch/Debug Target again and select a different target.

Clone this wiki locally