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 going to 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 can be asked for your environment, select C++, GDB, LD, DB, and then the C++ compiler again,
which in our case is G plus plus.
Now you're going to have some stuff happen and things are going to be changing colors and so forth.
Don't worry about any of that.
What we're going to do now is modify this file that was just created called Launch JSON.
We're going to make one minor change to it.
You close this window up so we can see it.
What we're going to do is we're going to change the entry for CW.
DX It's on line 14 on my system here, and we're going to change this from workspace folder to file
your name.
So I'm just going to copy that.
And paste it in here.
And that's all we're going to do.
Now, I'm going to 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 bit 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 going to initialize it to, let's say, 100 just so we have something to look at.
So it's still going to display hello from Project two, but now we want to prompt the user to enter
their favorite number.
So we'll put another display statement in here.
We'll just say Enter your favorite number and then we'll read in what type, and we're going to read
that into favorite number.
And then finally we're going to 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 want to 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 CP 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 and debug and nothing
will happen right away.
But we need to do is we need to set a breakpoint in our code.
This is where the debugger is going to stop when when it starts to debug our code.
So I want it to stop right there, right on line five, so you can just move along here and just select
the breakpoint, just click and it turns red and you can select as many breakpoints as you want.
Notice now that the breakpoints 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 recommend you do is when just when you're about to start debugging, click on that X right
there that erases all the breakpoints from everywhere.
Now, you don't see any down here, and then you could just come up to your project and put the breakpoint
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 play head 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 breakpoint.
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 100 and you can also hover over favorite number.
You'll see the 100 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 could 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, it 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 want to do is I want to execute one line at a time.
So I'm going to select step over and you can see the play head now moves to line six and the line five
executed and displayed something on the screen just like we expected.
All right.
And now it's going to ask me, what's your favorite number?
So I'm just going to type in something other than 100 so you can see a change.
This is 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 can just continue running the program and we're done.
So you see.
Hello from project to 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 going to show you how to use Visual Studio code with the source code that I
provide for the course.