(upbeat music)
(keyboard typing)
-: [Frank Mitropoulos] Hello, everyone.
In this video, we'll set up Visual Studio Code
to debug C++ programs.
We've already set up IntelliSense
and we've set up a build task.
So, now we're gonna set up the debug.
Where do we start?
It's really quite simple.
Select one of your C++ files, it doesn't matter which one.
And then, come up here to run and add configuration.
Now, you're gonna be asked for your environment,
select C++ GDB/LLDB, and then the C++ compiler again,
which in our case is G++.
Now, you're gonna have some stuff happen
and things are gonna be changing colors and so forth.
Don't worry about any of that.
What we're gonna do now is modify this file
that was just created called launch.json.
And we're gonna make one minor change to it.
Let me close this window up so we can see it.
What we're going to do is we're going to change
the entry for cwd.
It's on line 14 on my system here,
and we're gonna change this from workspace folder
to file your name.
So, I'm just gonna copy that and paste it in here.
And that's all we're gonna do.
Now, I'm gonna save this and I'm going to close it
since we don't need it any longer.
All right, let's debug our first program.
Let's modify project two's main CPP file, just a little
so we have a little information to see in the debugger.
So, what I'm going to do here, I'm going to add an integer
and let's just call this my favorite number
and I'm gonna initialize it to, let's say a hundred,
just so we have something to look at.
So, it's still going to display hello from project two,
but now we want it to prompt the user
to enter their favorite number.
So, we'll put another display statement in here
and we'll just say, enter your favorite number,
and then we'll read in what they type.
And we're gonna read that into favorite number.
And then, finally, we're gonna display
your favorite number is, and we'll display
what they typed in right back at them.
So, we'll say your favorite number is,
and we'll display what they entered.
Fave number, followed by a new line.
All right, so that's it.
So, we made a small change, but an important change
because we wanna be able to see
that favorite number change in the debugger.
All right, so how do we debug now?
Select the program you want to debug, again,
very important that we select that CPP file
that we want to debug.
In this case, this is the one we want to debug.
What we can do now is we can click that bug icon
right there where it says, run a debug,
and nothing will happen right away.
What we need to do is we need
to set a break point in our code.
This is where the debugger is going to stop
when it starts to debug our code.
So, I want it to stop right there, right on line five.
So, you could just move along here
and just select the break points.
Just click and it turns red and you can select
as many break points as you want.
Notice now that the break points show up here.
There is a glitch in Visual Studio Code
where sometimes when you have multiple breakpoints
across different projects, they all show here.
And what I'd recommend you do is just when you're about
to start debugging, click on that X right there.
That erases all the break points from everywhere.
Now, you don't see any down here,
and then, you could just come up to your project
and put the break point exactly where you want it.
So, now that we've done that,
we can click on this little green run icon right up here.
And when I do that, the debugger will engage.
You can see that there's a build happening
and now this bottom bar turns orange
and now there's a couple of things that have just happened.
Notice that this line five is highlighted
and the playhead is right there, right on line five.
That tells you that the debugger stopped at line five,
just like you asked it to.
That's where you put the break point.
So, line five has not been executed, line four has.
Notice that line four is our declaration of favorite number.
And if you look over here in your local variables,
there, you see its value.
That's pretty cool.
So, you can see favorite number is a hundred.
You can also hover over favorite number.
You'll see the hundred pop up.
You can do that anywhere it occurs.
Now, what we want to do is we want
to execute one line at a time.
And we can do that with this button right here.
You can see we've got a series of buttons here
that allow us to interact with the debugger.
This steps over the code, this steps into code.
And I discuss these in more detail throughout the course.
Here we can just stop the debugger
and here we can just continue.
So, what I wanna do is I want to execute one line at a time.
So, I'm going to select step over.
And you can see the playhead now moves to line six
and line five executed and displayed
something on the screen just like we expected.
All right, and now it's gonna,
it's asking you what's your favorite number.
So, I'm just gonna type in something
other than a hundred so you can see it change.
Let's just type in 45, and I'll press enter
and notice my local variable right here, 45.
That's pretty neat.
This allows me to step through my code
and see what's happening.
Maybe I made a mistake somewhere,
it's much easier to see it this way.
And then, we could just continue writing
the program and we're done.
So, you see hello from project two,
enter your favorite number.
I entered 45, and then it displayed
your favorite number is 45, just as we expected.
When you're done debugging, you can click over here,
go back to explore, and just debug
or code whatever project you like.
All right, so that shows you how to set up debugging.
So, at this point we've got building, we've got running,
and we've got debugging all set up.
In the next video, I'm gonna show you
how to use Visual Studio Code with the source code
that I provide for the course.