1
00:00:04,900 --> 00:00:10,450
In this video, I'll show you how to set up and use the Integrated Debugger in Visual Studio code.

2
00:00:10,450 --> 00:00:15,370
So for this example, I'm going to use Project two and I'm just going to make one little change right

3
00:00:15,370 --> 00:00:15,520
here.

4
00:00:15,520 --> 00:00:20,860
I'm going to initialize that number to 100 just so we can see it in the debugger in a minute.

5
00:00:21,130 --> 00:00:23,260
So let's create this.

6
00:00:23,260 --> 00:00:27,490
What we're going to do is we're going to create a launch JSON configuration file.

7
00:00:27,850 --> 00:00:31,870
First order of business is click on one of the main CP files.

8
00:00:31,870 --> 00:00:34,720
Since I'm using Project two, that's the one I'm going to select.

9
00:00:34,720 --> 00:00:40,690
So you can click on that file and then you're going to come up to run add configuration.

10
00:00:42,290 --> 00:00:45,050
And it's going to ask you again, what's your environment?

11
00:00:45,080 --> 00:00:50,900
We're going to always be using C++, GDB, WL, DB, so that's what I'm going to select.

12
00:00:51,680 --> 00:00:56,270
And again, I'm using GW 64 G plus plus.

13
00:00:56,270 --> 00:00:57,890
I'm going to select this guy right here.

14
00:01:01,730 --> 00:01:03,490
And we're going to come over here.

15
00:01:03,500 --> 00:01:04,250
Don't worry about this.

16
00:01:04,250 --> 00:01:08,150
You can just click that little red square right here and get out of here.

17
00:01:08,720 --> 00:01:13,130
The important thing is and we can come back over here as well to explore, just to clean this up a little

18
00:01:13,130 --> 00:01:13,570
bit.

19
00:01:13,580 --> 00:01:19,490
So the important thing is it has created a launch JSON file for us, and this is the one that's right

20
00:01:19,490 --> 00:01:22,490
here in the editor and this is the one that we're going to configure.

21
00:01:22,490 --> 00:01:25,040
And the configuration here is pretty easy.

22
00:01:25,940 --> 00:01:30,400
There's just one entry we need to change this one here in line 14, which is CW.

23
00:01:30,680 --> 00:01:32,600
That's our current working directory.

24
00:01:32,630 --> 00:01:40,130
What we're going to do is we're going to replace this with file name, and this time we're going to

25
00:01:40,130 --> 00:01:42,590
grab the dollar sign of the curlies as well.

26
00:01:43,040 --> 00:01:45,230
So we're going to put that right in there like that.

27
00:01:45,440 --> 00:01:45,980
That's it.

28
00:01:45,980 --> 00:01:46,900
So we're done with that.

29
00:01:46,910 --> 00:01:49,280
We can close up our launch JSON.

30
00:01:49,760 --> 00:01:52,880
Now what we're going to do is we want to debug this program.

31
00:01:52,880 --> 00:01:56,870
So in order to debug the program, we need some breakpoints.

32
00:01:56,870 --> 00:02:02,930
We need to tell the C++ debugger where to stop because otherwise it's just going to run the program.

33
00:02:02,930 --> 00:02:08,150
So it's got to stop so that we can examine variables, we can look at our logic, and we can step through

34
00:02:08,150 --> 00:02:14,600
the code one line at a time so that we can follow our logic and find mistakes or verify that what we

35
00:02:14,600 --> 00:02:15,530
did was correct.

36
00:02:15,530 --> 00:02:17,000
So let's do that.

37
00:02:17,000 --> 00:02:18,920
We can do this a couple of different ways.

38
00:02:18,920 --> 00:02:25,190
First of all, make sure that we've got this selected main step in whatever project we want to debug.

39
00:02:25,220 --> 00:02:30,680
In this case, we're debugging project two, so make sure your default is selected main DHCP.

40
00:02:30,710 --> 00:02:32,450
Then we can come up here to run.

41
00:02:32,450 --> 00:02:33,830
Start debugging.

42
00:02:35,030 --> 00:02:41,660
Or we can come over here on the left side here again, select main app and click that bug right there.

43
00:02:41,900 --> 00:02:42,830
Run and debug.

44
00:02:42,830 --> 00:02:44,030
That's what I'm going to do.

45
00:02:44,740 --> 00:02:48,070
And now what we need to do is we need to add a breakpoint to our code.

46
00:02:48,070 --> 00:02:52,810
And you can see over here in the gutter here, I'm sliding through and you can see that red dot coming

47
00:02:52,810 --> 00:02:53,040
around.

48
00:02:53,050 --> 00:02:53,950
That's a breakpoint.

49
00:02:53,950 --> 00:02:58,210
So if I want to put the breakpoint right about there, it'll light up.

50
00:02:58,210 --> 00:03:02,080
It'll also show up down here that says we've got a breakpoint.

51
00:03:02,560 --> 00:03:08,800
Visual Studio code has a little glitch sometimes where it captures breakpoints from a lot of the projects.

52
00:03:09,130 --> 00:03:10,450
Just the one you're working with.

53
00:03:10,450 --> 00:03:16,210
And an easy way to deal with that is just to before you start debugging, just come over here and click

54
00:03:16,210 --> 00:03:17,290
that X right there.

55
00:03:17,290 --> 00:03:21,460
That'll clear up all the breakpoints everywhere, and then you can just set the breakpoint where you

56
00:03:21,460 --> 00:03:21,880
want.

57
00:03:21,880 --> 00:03:23,980
So I'm going to set the breakpoint again right there.

58
00:03:24,460 --> 00:03:26,890
So now that we've done that, we're going to run the program.

59
00:03:26,890 --> 00:03:32,140
And what we can do is we can come up here and click on that little green arrow and you'll see what happens.

60
00:03:32,140 --> 00:03:34,240
The debugger comes into play.

61
00:03:34,330 --> 00:03:36,340
A lot of things seem to happen all at once.

62
00:03:36,340 --> 00:03:42,130
We've got this little menu bar right here, this bar of icons that allows us to step through our code

63
00:03:42,130 --> 00:03:46,360
one line at a time, stop the code, we run the code and so forth.

64
00:03:46,360 --> 00:03:51,910
We also have this variables window that popped up over here on the left, and this is showing the values

65
00:03:51,910 --> 00:03:53,050
of our local variables.

66
00:03:53,050 --> 00:03:53,920
Look at NUM.

67
00:03:53,920 --> 00:03:57,130
It's initialized to 100, so it shows up right over here.

68
00:03:58,060 --> 00:04:03,720
Also we can use our cursor and just hover over here and you can see its values 100.

69
00:04:03,730 --> 00:04:09,190
So it lets us inspect the value of that variable as we're running the code, which is really neat.

70
00:04:09,220 --> 00:04:13,420
So right here, this is the other line that shows you where I'm at in the program.

71
00:04:13,630 --> 00:04:17,589
So you can see that yellow bar shows us exactly where our program is.

72
00:04:17,589 --> 00:04:19,720
This line has not executed yet.

73
00:04:19,959 --> 00:04:24,370
So if we want to execute it, we can come over here and step over it or step into it.

74
00:04:24,370 --> 00:04:26,640
And we'll talk about all this in the course.

75
00:04:26,680 --> 00:04:31,180
And in this case, I want to step over it and notice the output came out down here.

76
00:04:31,180 --> 00:04:32,600
So that's the line we're at.

77
00:04:32,620 --> 00:04:36,430
So I'm going to step over it again and now we're going to get the input from the user.

78
00:04:36,430 --> 00:04:38,410
So let's say I type in 1200.

79
00:04:39,370 --> 00:04:44,620
Now, as soon as I press enter, that 1200 will be red into num, right?

80
00:04:44,620 --> 00:04:46,680
So my variables should change.

81
00:04:46,700 --> 00:04:51,840
I'm going to press enter and now notice that number up here is 1200 and I can also hover over.

82
00:04:51,910 --> 00:04:53,570
I'm over here and it's 100.

83
00:04:53,590 --> 00:04:56,850
So this is a really, really powerful feature debugger.

84
00:04:56,860 --> 00:04:58,510
The are are great.

85
00:04:58,510 --> 00:04:59,410
We use them all the time.

86
00:04:59,410 --> 00:05:00,340
We don't overuse them.

87
00:05:00,340 --> 00:05:05,110
If it's a really simple program and you've got simple logic and you can test your code easily, then

88
00:05:05,110 --> 00:05:07,270
there's real there's no real need to debug it.

89
00:05:07,270 --> 00:05:11,890
But sometimes it comes in really handy when we're trying to figure out a problem or looking at some

90
00:05:11,890 --> 00:05:15,550
really complex logic that we want to walk through step by step.

91
00:05:15,670 --> 00:05:16,640
So that's it.

92
00:05:16,660 --> 00:05:21,520
Now, if we want to stop the program, we could just click that little red box right here to stop.

93
00:05:22,270 --> 00:05:27,340
If we want to just continue full blast and skip all the breakpoints, we can click that little arrow

94
00:05:27,340 --> 00:05:28,000
right here.

95
00:05:28,720 --> 00:05:32,290
Or we could just click another few times until the program ends.

96
00:05:32,440 --> 00:05:35,920
And let me just stop it here so you can see what that looks like.

97
00:05:35,920 --> 00:05:36,760
So I stop it.

98
00:05:38,120 --> 00:05:38,290
Right.

99
00:05:38,330 --> 00:05:39,320
So now it's on notice.

100
00:05:39,320 --> 00:05:41,270
It's changed back to blue down here.

101
00:05:41,360 --> 00:05:43,220
And I can come back to my.

102
00:05:44,200 --> 00:05:46,420
Explorer and I can see everything that I'm doing.

103
00:05:46,420 --> 00:05:47,320
So there you go.

104
00:05:47,320 --> 00:05:49,090
That's how the debugger works.

105
00:05:49,090 --> 00:05:54,070
In the next video, what I'm going to do is I'm going to show you how to use Visual Studio code with

106
00:05:54,070 --> 00:05:56,740
the source code that I've provided for the course.

107
00:05:56,740 --> 00:06:02,740
The source code that I provided for the course is in the format of code light projects, but that's

108
00:06:02,740 --> 00:06:03,040
okay.

109
00:06:03,070 --> 00:06:07,510
The work in Visual Studio code as well, and I'll show you exactly how to use them in the next video.

