Debug a C++ project in VS Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] visual studio code uses the c plus compiler and debugger you have installed on your computer to build and debug your program debug configurations are defined in a file called launch.json launch.json lives in your workspaces.bscode folder let's start by creating launch.json and defining the debug configuration for our hello world program let's add a new configuration choose a debugger environment then choose from the pre-defined debugging configurations the idea here is whether you are working on linux mac or windows your debug configuration will invoke the c plus plus debugger you have installed on your computer here we are showing you a debug configuration that invokes lldb on mac our docs call out the specifics for the other platforms this creates and opens our launch.json file let's walk through this default configuration the program setting specifies the program you want to debug here it is set to the active file folder an active file name so if helloworld.cpp is the active file this looks for the hello world executable in our project folder args is an array of command line arguments to pass to the program when it is launched by default the c plus extension won't add any breakpoints to your source code and the stop at entry value is set to false if you want the debugger to stop on the main method when you start debugging change stop at entry to true mi mode indicates to the debugger that vs code will connect to either gdb or lldb there are some other properties you can add to customize your gdb or lldb configuration such as mi debugger path and mi debugger args mi debugger path is the path to the debugger when left unspecified it will search your computer's path variable for a debugger either gdb or lldb depending on what mi mode is set to mi debugger args specifies additional arguments to pass to the debugger let's save our changes and go back to helloworld.cpp remember our launch configuration requires helloworld.cpp to be the active file in the editor to debug our hello world program to start debugging use the keyboard shortcut f5 our debugging session has started open the debug console to see the output that shows the debugger is up and running this is also where the output from our program will be printed the editor highlights the first statement in our main function this is the breakpoint the c plus extension automatically set for us since we set stop at entry to true the control panel has six actions continue which will continue running the program until the next breakpoint step over to go to the next line of code skipping over function calls step into to step into a function step out to step out of a function restart to restart your debug session and stop to stop the debugging session if we step over to the next line you'll see the value of our msg variable reflected in the variables window let's step over again now we see the variable in our for loop word in our locals list the value of word changes as we iterate through the for loop note that no output will appear in the debug console until the last c out statement completes because the newline character is required to flush the buffer variable values can be modified with the set value action from the variables context menu you can also add a variable to watch which lets you watch the value of the variable change over time the variable values you see are relative to the selected stack frame in the call stack section for us it's the main function in helloworld.cpp let's click continue to finish running the program and end the debugging session that's how you use launch configurations to invoke the c plus debugger you have installed on your computer the next time you want to debug this program all you have to do is hit f5
Info
Channel: Visual Studio Code
Views: 51,625
Rating: undefined out of 5
Keywords: vscode, c++, visual studio code
Id: G9gnSGKYIg4
Channel Id: undefined
Length: 4min 49sec (289 seconds)
Published: Mon Mar 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.