[MUSIC]
>> Visual Studio Code uses the C++ 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.vscode 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 the debugger environment,
then choose from the predefined debugging configurations.
The idea here is whether you are
working on Linux, Mac, or Windows,
your debug configuration will invoke
the C++ 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 and active file name.
So if helloworld.cpp is the active file,
this looks for the helloworld 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++ extension won't add any breakpoints
to your source code and the stopAtEntry value is set to false.
If you want the debugger to stop on
the main method when you start debugging,
change stopAtEntry to true.
MIMode 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 miDebuggerPath and miDebuggerArgs.
MiDebuggerPath 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 MIMode is set to.
miDebuggerArgs 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++ extension
automatically set for us since we set stopAtEntry 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 cout 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++ debugger you have installed on your computer.
The next time you want to debug this program,
all you have to do is hit F5.
[MUSIC]