(upbeat music)
Frank: In this video, I'll show you how to set up
and use the integrated debugger in Visual Studio Code.
So for this example, I'm going to use project two
and I'm just gonna make one that'll change right here.
I'm gonna initialize that number to 100
just so we can see it in the debugger in a minute.
So let's create this.
What we're gonna do is we're gonna create
a launch.json configuration file.
First order of business is
click on one of the main CPP files.
Since I'm using project two,
that's the one I'm going to select.
So you can click on that file
and then you're gonna come up to Run, Add Configuration,
and it's going to ask you again, what's your environment?
We're going to always be using C++ GDB/LLDB,
so that's what I'm going to select.
And again, I'm using mingw64 g++.
I'm gonna select this guy right here.
And we're gonna come over here.
Don't worry about this.
You could just click that little red square right here
and get out of here.
The important thing is,
and we can come back over here as well to explore
or just to clean this up a little bit,
so the important thing is
it has created a launch.json file for us
and this is the one that's right here in the editor
and this is the one that we're gonna configure.
And the configuration here is pretty easy.
There's just one entry we need to change,
this one here on line 14, which is cwd.
That's our current working directory.
What we're going to do is we're gonna replace this
with fileDirname
and this time we're gonna grab the dollar sign
and the curly as well.
So we're gonna put that right in there, like that.
That's it, so we're done with that.
We can close up our launch.json.
Now what we're going to do is we want to debug this program.
So in order to debug the program, we need some break points.
We need to tell the C++ debugger where to stop
because otherwise it's just gonna run the program.
So it's gotta stop so that we can examine variables,
we can look at our logic,
and we can step through the code one line at a time
so that we can follow our logic and find mistakes
or verify that what we did was correct.
So let's do that.
We can do this a couple of different ways.
First of all, make sure that we've got this selected,
main.cpp, in whatever project we want to debug.
In this case, we're debugging project two,
so make sure your default has selected main.cpp.
Then we can come up here to Run, Start Debugging,
or we can come over here on the left side here.
Again, select main.cpp,
and click that bug right there, right, run and debug.
That's what I'm going to do.
And now what we need to do
is we need to add a break point to our code.
And you can see over here in the gutter here,
I'm sliding through
and you can see that red dot coming around.
That's a break point.
So if I wanna put the break point right about there,
it'll light up.
It'll all show up down here
that says we've got a break point.
Visual Studio Code has a little glitch sometimes
where it captures break points from a lot of the projects
instead of just the one you're working with.
And an easy way to deal with that is just to
before you start debugging
just come over here and click that X right there.
That'll clear up all the break points everywhere
and then you could just set the break point where you want.
So I'm gonna set the break point again right there.
So now that we've done that, we're gonna run the program
and what we can do is we can come up here
and click on that little green arrow
and you'll see what happens.
The debugger comes into play.
A lot of things seem to happen all at once.
We've got this little menu bar right here,
this bar of icons that allows us to step through our code
one line at a time, stop the code,
rerun the code, and so forth.
And we also have this variables window
that popped up over here on the left.
And this is showing the values of our local variables.
Look at num, it's initialized to 100,
so it shows up right over here.
Also, we can use our cursor and just hover over num here
and you can see its value is 100.
So it lets us inspect the value of that variable
as we're running the code, which is really neat.
So right here, this yellow line,
that shows you where I'm at in the programs.
So you could see that yellow bar shows us
exactly where our program is.
This line has not executed yet.
So if we want to execute it,
we can come over here and step over it or step into it.
And I'll talk about all this in the course.
And in this case, I wanna step over it
and notice the output came out down here.
So that's the line we're at.
So I'm gonna step over it again
and now we're gonna get the input from the user.
So let's say I type in 1,200.
Now as soon as I press Enter,
that 1,200 will be read into num, right?
So my variables should change.
I'm gonna press Enter
and now notice that num up here is 1,200.
I can also hover over num over here and it's 1,200.
So this is a really, really powerful feature.
Debuggers are great. We use them all the time.
We don't overuse them.
If it's a really simple program and you've got simple logic
and you can test your code easily,
there's no real need to debug it,
but sometimes it comes in really handy
when we're trying to figure out a problem
or looking at some really complex logic
that we wanna walk through step by step.
So that's it.
Now, if we wanna stop the program,
we could just click that little red box right here to stop.
If we want to just continue full blast
and skip all the break points,
we can click that little arrow right here
or we could just click on it a few times
until the program ends.
And let me just stop it here
so you can see what that looks like.
So I stop it.
So now it's gone, notice it's changed back to blue down here
and I can come back to my Explorer
and I can see everything that I'm doing.
So there you go, that's how the debugger works.
In the next video, what I'm going to do
is I'm gonna show you how to use Visual Studio Code
with the source code that I've provided for the course.
The source code that I provided for the course
is in the format of CodeLite projects, but that's okay.
They'll work in Visual Studio Code as well,
and I'll show you exactly how to use them in the next video.