How to Setup VS Code for Debugging C++ on Windows

Published on Mar 06, 2022 by Arun Michael Dsouza

How to Setup VS Code for Debugging C++ on Windows

VS Code has gained a lot of traction over the past few years due to its ease of use and extensibility. It offers a wide variety of plugins to use and customize workflows for any language or technology.

One of the most powerful features of VS Code is its debugger. It has Node JS runtime debugging support which can be used to debug JavaScript, TypeScript and other languages that can be transpiled to JavaScript. For debugging languages other than JavaScript, their corresponding debugging extension needs to be installed in VS Code.

In this blog post, we will be looking at how we can set up VS Code to debug C++ on Windows. The steps are pretty much the same for other operating systems but they have been covered in separate blog posts that you can check out below -

Setting up VS Code for C++ Development

Since C++ is a compiled language, what we first need is a compiler. The compiler will compile the code and execute it, then we can attach a debugger to it and start debugging the code that is being executed.

If you already have gcc or g++ installed on your system, you can open up a command prompt or terminal window and use these commands to verify if a version of these compilers is installed -

g++ --version
gcc --version

If there’s no C++ compiler installed on your system, you can install the latest versions of gcc and g++ using MSYS2. Once installed, open MSYS2 and use the following command to install the Mingw-w64 toolchain -

pacman -S --needed base-devel mingw-w64-x86_64-toolchain

This will install the native binaries for gcc and g++.

Now we need to add the Mingw-w64 bin directory to the path environment variable. To do this type Edit environment variables for your account in the Windows search bar and press enter. Under User variables, select the Path variable and click Edit. Click on New and paste the path of the Mingw-w64 bin directory and click OK. The directory path might look something like this -

C:\msys64\mingw64\bin

Please note that the exact path depends on the installed version and location of MSYS2.

Note: In this blog post, we will be using the g++ compiler.

Now that we have a C++ compiler installed, the next step would be to install the C/C++ Extension for VS Code which offers intellisense and debugging support. Once installed restart VS Code.

VS Code C/C++ Extension
VS Code C/C++ Extension

Creating a C++ Debug Configuration

Open the C++ source file that you want to debug in VS Code. To start the debugging process we need to create a debug configuration that will help us in debugging and executing C++. Go to the Run and Debug tab and click on the Run and Debug button, this will open up the environment selection menu. Or you can press F5 alternatively.

VS Code supports the GDB debugger on Windows, so click on the C++ (GDB/LLDB) menu item.

Selecting a Debug Environment
Selecting a Debug Environment

This will open up another menu to select the compiler configuration with a list of supported compilers in the list.

Selecting a Compiler Configuration
Selecting a Compiler Configuration

Selecting a compiler will create the debug configuration and save it inside the .vscode directory with two files -

launch.json

 1{
 2  "version": "0.2.0",
 3  "configurations": [
 4    {
 5      "name": "g++.exe - Build and debug active file",
 6      "type": "cppdbg",
 7      "request": "launch",
 8      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
 9      "args": [],
10      "stopAtEntry": false,
11      "cwd": "${fileDirname}",
12      "environment": [],
13      "externalConsole": false,
14      "MIMode": "gdb",
15      "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
16      "setupCommands": [
17        {
18          "description": "Enable pretty-printing for gdb",
19          "text": "-enable-pretty-printing",
20          "ignoreFailures": true
21        },
22        {
23          "description": "Set Disassembly Flavor to Intel",
24          "text": "-gdb-set disassembly-flavor intel",
25          "ignoreFailures": true
26        }
27      ],
28      "preLaunchTask": "C/C++: g++.exe build active file"
29    }
30  ]
31}

And tasks.json

 1{
 2  "tasks": [
 3    {
 4      "type": "cppbuild",
 5      "label": "C/C++: g++.exe build active file",
 6      "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
 7      "args": ["-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
 8      "options": {
 9        "cwd": "${fileDirname}"
10      },
11      "problemMatcher": ["$gcc"],
12      "group": {
13        "kind": "build",
14        "isDefault": true
15      },
16      "detail": "Task generated by Debugger."
17    }
18  ],
19  "version": "2.0.0"
20}

It will also compile the currently opened C++ source file, just make sure that the file is open while selecting the compiler. You will also notice that if you have a breakpoint set, the debug process will stop on it.

C++ Breakpoint Being Hit
C++ Breakpoint Being Hit

Tip: If you want to begin debugging right when the program starts, you can set the stopAtEntry property in your launch.json file to true. This will stop the program on the main() function.

So there you have it! You can now start building and debugging C++ on Windows with VS Code. If you want to set it up for a different platform you can check out the following blog posts -

If you have any questions or suggestions, please leave a comment down below. Also if you want to stay up to date with the latest happenings at example.com, feel free to follow me on Twitter.

Cheers!

Arun Michael Dsouza

Arun Michael Dsouza

Lorem ipsum dolor, sit amet consectetur adipisicing elit. Veniam recusandae consequatur necessitatibus modi nostrum ullam.