(upbeat music)
(keyboard types)
Frank: 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.
We can inspect variables as we do that.
Let's use the same project that we wrote
in the previous video, and I'm gonna debug project two here.
I'm just gonna make one little change to it.
I'm gonna initialize favorite number to a 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 any main.cpp file.
What we're doing now is we're configuring the debugger.
I'm gonna select main.cpp, then I'm going to go up to Run,
and add a configuration.
I'm going to select C++ GDB and my g++ compiler again.
Now, 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.
You can see that a new file was created.
It's called launch.json.
This is the one that we're gonna modify slightly
to get our debugger to work.
The most important thing to do here is
we want to change line 9, which is the type,
and we want to change that from cppdbg to lldb.
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.
Let me get rid of that.
We'll get rid of these three lines as well.
That's what we've got now on line 13.
Cwd should be fileDirname.
If it's anything other than that, be sure to change it
to fileDirname, and that's it, we're done.
Again, make sure there's no squigglies
and no errors in here.
I just save that
and I'm going to close it because we're done with it.
Now, we're ready to debug.
To start the debugger,
select the file that you want to debug.
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 in project one.
I want project two's main.cpp, so I've just selected it
Here it is in my editor.
To start the debugger,
you click on this bug icon right here, Run and Debug.
I'll do that now.
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 CodeLite 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 debugger 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.
Let's say that I wanted to stop right on that line.
Now when the debugger starts executing,
it's gonna run the program and stop here,
and then we can control it from there.
Also, notice that when I select that breakpoint,
it shows up down here.
Right, main.cpp's 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.
Then, you can just come back up and select your breakpoint,
and you're good to go.
Once you're ready to start debugging, we click
on that green play button right up here on the top left.
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.
This area right here is where your variables show up.
You can see my favorite number variable is a 100,
and that's exactly what I initialized it to.
We can also hover over our variable names just like that.
You can see the 100 pop up.
That's the value and that's the current value.
If we change it, that number's going to change.
You can see that line six executed right down here
in the terminal: Hello from project two.
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.
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.
I'm gonna 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 playhead is here on line nine.
Line nine has not executed yet,
but also I noticed I entered 45,
but look what happened over here
on the top left in my variables: favorite number is now 45.
Again, I can hover over at any word that it's being used
to see its value.
This is really handy.
Sometimes programming gets really complex.
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.
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.
You can see it says here, "Your favorite number is 45."
At this point, we're done. This bottom bar turns blue again.
If we want to continue editing and so forth,
we can just click Explorer 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.