Welcome back.
In this video, I'm going to show you how to set up the debugger so that we can debug C++ programs in
Visual Studio code.
The debugger is a great tool that allows us to execute our code one line at a time, and we can inspect
variables as we do them.
So let's use the same project that we wrote in the previous video, and I'm going to debug Project two
here.
I'm just going to make one little change to it.
I'm going to initialize favorite number to 100 just so we can see it and see it when it changes.
All right, so where do we start?
Well, it's pretty easy.
You just select the file, any main CPP file.
What we're doing now is we're configuring the debugger, so I'm going to select main CPP, then I'm
going to go through run and add a configuration.
And I'm going to select C plus plus gdb.
And my G plus plus compiler again.
Then you'll see some stuff happen and you'll see this little menu bar pop up.
Just click that little red box to stop the debugger and we'll finish configuring it.
I'm going to close this window down here to give us some more room.
And you can see that a new file was created.
It's called Launch JSON.
And this is the one that we're going to modify slightly to get our debugger to work.
The most important thing to do here is we want to change line nine, which is the type, and we want
to change that from CP Dpkg to LSB.
Now when we do that, you'll see that we have some errors pop up here on lines 13, 15, 16 and 17.
We want to delete those lines completely.
So let me get rid of that and we'll get rid of these three lines as well.
And that's what we've got now on line 13 should be filed.
Your name.
If it's anything other than that, be sure to change it, to file your name and that's it.
We're done.
Make sure again, make sure there's no squiggles and no errors in here.
I just saved that.
And I'm going to close it because we're done with it.
So now we're ready to debug to start the debugger.
Select the file that you want to debug.
So in this case, I want to debug main CPP in Project two.
If I wanted to debug Project one, I would click on main CPP and Project one.
So I don't I want Project Two's main CPP.
So I've just selected it and here it is in my editor.
To start the debugger, you click on this bug icon right here, run and debug.
So I'll do that now and you'll notice a couple of things happen.
Well, one is you have this play arrow up here and you have this information area where you have variables
and watches and call stacks and breakpoints.
I'll cover these during the course.
I'll cover them in code light in the course, but it's the same concept in Visual Studio code.
What we need to do first is set a breakpoint.
This is the place where the bugger is going to stop executing our code.
You can do that right over here on the left side.
If you just hover over this left gutter here, you see that red dot that keeps popping up?
Well, you could just select that.
So let's say that I wanted to stop right on that line.
So now when the debugger starts executing, it's going to run the program and stop here and then we
can control it from there.
Also, notice that when to select that breakpoint, it shows up down here.
Bright main KPIs breakpoint in Project two.
Now Visual Studio code has a glitch where sometimes all the breakpoints and all the projects show up
here.
My recommendation is when you start your debugging, as soon as you click on that bug icon, just come
down here and click that X if there's anything there that clears out all the breakpoints everywhere,
and then you can just come back up and click select your breakpoint and you're good to go.
So once you're ready to start debugging, we click on that green play button right up here on the top
left.
And when I do that, you see this little menu option here comes up with these icons that control the
debugger.
The bottom bar down here turns orange, and this area right here is where your variables show up.
You can see my favorite number of variables, 100, and that's exactly what I initialized it to.
We can also hover over our variable names just like that, and you can see the 100 pop up.
That's the value and that's the current value.
If we change it, that number is going to change.
What I can do next is and you can see that line six executed right down here in the terminal.
Hello from project two.
So now what I can do is I can click these buttons and I'll explain these buttons during the course.
But this one right here just steps over the code.
It executes that statement.
So if I want to execute that statement now, the program is basically waiting for us to type something
in.
Right, right here.
We have to enter our favorite number.
So I'm going to enter in something other than 100 so that we can see favorite number change.
Let me type in 45 and press enter.
You can see now that the play head is here on line nine.
Line nine has not executed yet, but also notice I entered 45.
But look what happened over here on the top left and my variables favorite number is now 45.
And again, I can hover over it anywhere that it's being used to see its value.
This is really handy.
Sometimes programming gets really complex and it's very helpful to be able to execute your code one
line at a time and see your variables, see the results of your calculations visually.
And it can be very, very useful when we're done.
We can just continue the program or we can stop the program right here in this case, I'm just going
to continue and you can see it says here, your favorite number is 45.
And at this point, we're done.
This bottom bar turns blue again.
And if we want to continue editing and so forth, we can just click explore and go right back to our
files.
So that's it.
At this point, we've set up Visual Studio code for building a task.
We know how to execute our executable and we know how to debug our project.
In the next video, I'll show you how we can use the source code that I provide for the course within
Visual Studio code.
I'll see you there.