How to Run Debug in Visual Studio for C++

Lucas Suryana
3 min readMar 20, 2024

--

What is a debugger?

Debugging plays a crucial role in software development. Debugger is a tool that allows us to stop the execution of our code in the preselected location, inspect the program’s state, and step through the code line by line.

In this article, I will use Visual Code Studio’s debugger with my C++ file.

Steps to perform debugging in Visual Code Studio

  1. Install C+/C++ Extension in your own Visual Code Studio
C/C++ Extension

2. Create a new folder, for instance, INTRO_CPP, and create a CPP file called test.cpp

test.cpp
#include <iostream>
#include <string>
#include <vector>

int main() {
std::vector<std::string> brothers;
brothers.push_back("David");
brothers.push_back("Ethan");
brothers.push_back("Adam");

for (int i = 0; i < brothers.size(); i++) {
std::string const& brother = brothers[i];
std::cout << "Hello " << brother << "!\n";
}
}

3. Run the code by executing ‘g++ test.cpp" and then ‘./a.out’ on your terminal. If the code runs well, you will get the result as follows:

result of test.cpp

4. Now, we will select a part of the code that we want to inspect. To do this, click on the left side of the line number in the test.cpp file. For instance, if I want to evaluate line 13, click the left side until it shows the red dot.

red dot indicates line of code to inspect

5. Click Debug C/C++ file and choose “C/C++ g++ build and debug active file” to start the debugging process.

debug c/c++ file

6. If you do all the steps correctly, you will get the results as follows:

debugging result

Below Variables, you can see that a variable called ‘brothers’ is created that consists of three strings: “David”, “Ethan”, and “Adam”. We can also see the values of ‘i: 0’ and ‘brother: ‘David’’ that indicate the first value of the loop. If we click the ‘Continue’ button, then it will continue the iteration process.

continue button

You can also watch the video tutorial here.

Thanks for reading/watching!

--

--

Lucas Suryana

PhD Student @TU Delft — Developing a safer and a more acountable automated vehicle