1 00:00:00,123 --> 00:00:02,790 (upbeat music) 2 00:00:04,920 --> 00:00:06,810 Frank: In this video, I'll show you how to set up 3 00:00:06,810 --> 00:00:10,590 and use the integrated debugger in Visual Studio Code. 4 00:00:10,590 --> 00:00:13,650 So for this example, I'm going to use project two 5 00:00:13,650 --> 00:00:15,510 and I'm just gonna make one that'll change right here. 6 00:00:15,510 --> 00:00:18,750 I'm gonna initialize that number to 100 7 00:00:18,750 --> 00:00:21,210 just so we can see it in the debugger in a minute. 8 00:00:21,210 --> 00:00:23,280 So let's create this. 9 00:00:23,280 --> 00:00:24,360 What we're gonna do is we're gonna create 10 00:00:24,360 --> 00:00:27,930 a launch.json configuration file. 11 00:00:27,930 --> 00:00:29,790 First order of business is 12 00:00:29,790 --> 00:00:31,890 click on one of the main CPP files. 13 00:00:31,890 --> 00:00:33,180 Since I'm using project two, 14 00:00:33,180 --> 00:00:34,800 that's the one I'm going to select. 15 00:00:34,800 --> 00:00:36,540 So you can click on that file 16 00:00:36,540 --> 00:00:40,623 and then you're gonna come up to Run, Add Configuration, 17 00:00:42,390 --> 00:00:45,240 and it's going to ask you again, what's your environment? 18 00:00:45,240 --> 00:00:49,200 We're going to always be using C++ GDB/LLDB, 19 00:00:49,200 --> 00:00:51,750 so that's what I'm going to select. 20 00:00:51,750 --> 00:00:56,280 And again, I'm using mingw64 g++. 21 00:00:56,280 --> 00:00:58,130 I'm gonna select this guy right here. 22 00:01:01,710 --> 00:01:03,480 And we're gonna come over here. 23 00:01:03,480 --> 00:01:04,313 Don't worry about this. 24 00:01:04,313 --> 00:01:06,930 You could just click that little red square right here 25 00:01:06,930 --> 00:01:08,700 and get out of here. 26 00:01:08,700 --> 00:01:10,020 The important thing is, 27 00:01:10,020 --> 00:01:11,940 and we can come back over here as well to explore 28 00:01:11,940 --> 00:01:13,740 or just to clean this up a little bit, 29 00:01:13,740 --> 00:01:14,850 so the important thing is 30 00:01:14,850 --> 00:01:18,240 it has created a launch.json file for us 31 00:01:18,240 --> 00:01:20,580 and this is the one that's right here in the editor 32 00:01:20,580 --> 00:01:22,620 and this is the one that we're gonna configure. 33 00:01:22,620 --> 00:01:24,993 And the configuration here is pretty easy. 34 00:01:25,950 --> 00:01:27,720 There's just one entry we need to change, 35 00:01:27,720 --> 00:01:30,660 this one here on line 14, which is cwd. 36 00:01:30,660 --> 00:01:32,760 That's our current working directory. 37 00:01:32,760 --> 00:01:36,100 What we're going to do is we're gonna replace this 38 00:01:37,140 --> 00:01:39,450 with fileDirname 39 00:01:39,450 --> 00:01:41,280 and this time we're gonna grab the dollar sign 40 00:01:41,280 --> 00:01:43,110 and the curly as well. 41 00:01:43,110 --> 00:01:45,510 So we're gonna put that right in there, like that. 42 00:01:45,510 --> 00:01:47,010 That's it, so we're done with that. 43 00:01:47,010 --> 00:01:49,830 We can close up our launch.json. 44 00:01:49,830 --> 00:01:53,040 Now what we're going to do is we want to debug this program. 45 00:01:53,040 --> 00:01:56,880 So in order to debug the program, we need some break points. 46 00:01:56,880 --> 00:02:01,110 We need to tell the C++ debugger where to stop 47 00:02:01,110 --> 00:02:02,940 because otherwise it's just gonna run the program. 48 00:02:02,940 --> 00:02:05,700 So it's gotta stop so that we can examine variables, 49 00:02:05,700 --> 00:02:07,200 we can look at our logic, 50 00:02:07,200 --> 00:02:10,139 and we can step through the code one line at a time 51 00:02:10,139 --> 00:02:13,290 so that we can follow our logic and find mistakes 52 00:02:13,290 --> 00:02:15,600 or verify that what we did was correct. 53 00:02:15,600 --> 00:02:17,100 So let's do that. 54 00:02:17,100 --> 00:02:18,990 We can do this a couple of different ways. 55 00:02:18,990 --> 00:02:22,020 First of all, make sure that we've got this selected, 56 00:02:22,020 --> 00:02:25,260 main.cpp, in whatever project we want to debug. 57 00:02:25,260 --> 00:02:27,360 In this case, we're debugging project two, 58 00:02:27,360 --> 00:02:30,810 so make sure your default has selected main.cpp. 59 00:02:30,810 --> 00:02:33,843 Then we can come up here to Run, Start Debugging, 60 00:02:34,980 --> 00:02:38,010 or we can come over here on the left side here. 61 00:02:38,010 --> 00:02:39,930 Again, select main.cpp, 62 00:02:39,930 --> 00:02:42,840 and click that bug right there, right, run and debug. 63 00:02:42,840 --> 00:02:44,790 That's what I'm going to do. 64 00:02:44,790 --> 00:02:45,623 And now what we need to do 65 00:02:45,623 --> 00:02:48,210 is we need to add a break point to our code. 66 00:02:48,210 --> 00:02:49,860 And you can see over here in the gutter here, 67 00:02:49,860 --> 00:02:51,240 I'm sliding through 68 00:02:51,240 --> 00:02:53,010 and you can see that red dot coming around. 69 00:02:53,010 --> 00:02:54,090 That's a break point. 70 00:02:54,090 --> 00:02:57,270 So if I wanna put the break point right about there, 71 00:02:57,270 --> 00:02:58,260 it'll light up. 72 00:02:58,260 --> 00:03:00,420 It'll all show up down here 73 00:03:00,420 --> 00:03:02,550 that says we've got a break point. 74 00:03:02,550 --> 00:03:05,550 Visual Studio Code has a little glitch sometimes 75 00:03:05,550 --> 00:03:08,790 where it captures break points from a lot of the projects 76 00:03:08,790 --> 00:03:10,650 instead of just the one you're working with. 77 00:03:10,650 --> 00:03:13,470 And an easy way to deal with that is just to 78 00:03:13,470 --> 00:03:14,880 before you start debugging 79 00:03:14,880 --> 00:03:17,340 just come over here and click that X right there. 80 00:03:17,340 --> 00:03:19,650 That'll clear up all the break points everywhere 81 00:03:19,650 --> 00:03:21,990 and then you could just set the break point where you want. 82 00:03:21,990 --> 00:03:24,600 So I'm gonna set the break point again right there. 83 00:03:24,600 --> 00:03:26,880 So now that we've done that, we're gonna run the program 84 00:03:26,880 --> 00:03:28,290 and what we can do is we can come up here 85 00:03:28,290 --> 00:03:30,660 and click on that little green arrow 86 00:03:30,660 --> 00:03:32,100 and you'll see what happens. 87 00:03:32,100 --> 00:03:34,230 The debugger comes into play. 88 00:03:34,230 --> 00:03:36,480 A lot of things seem to happen all at once. 89 00:03:36,480 --> 00:03:38,670 We've got this little menu bar right here, 90 00:03:38,670 --> 00:03:42,240 this bar of icons that allows us to step through our code 91 00:03:42,240 --> 00:03:44,790 one line at a time, stop the code, 92 00:03:44,790 --> 00:03:46,050 rerun the code, and so forth. 93 00:03:46,050 --> 00:03:48,180 And we also have this variables window 94 00:03:48,180 --> 00:03:50,370 that popped up over here on the left. 95 00:03:50,370 --> 00:03:53,040 And this is showing the values of our local variables. 96 00:03:53,040 --> 00:03:55,380 Look at num, it's initialized to 100, 97 00:03:55,380 --> 00:03:57,183 so it shows up right over here. 98 00:03:58,050 --> 00:04:01,800 Also, we can use our cursor and just hover over num here 99 00:04:01,800 --> 00:04:03,840 and you can see its value is 100. 100 00:04:03,840 --> 00:04:07,020 So it lets us inspect the value of that variable 101 00:04:07,020 --> 00:04:09,390 as we're running the code, which is really neat. 102 00:04:09,390 --> 00:04:11,010 So right here, this yellow line, 103 00:04:11,010 --> 00:04:13,740 that shows you where I'm at in the programs. 104 00:04:13,740 --> 00:04:15,720 So you could see that yellow bar shows us 105 00:04:15,720 --> 00:04:17,670 exactly where our program is. 106 00:04:17,670 --> 00:04:20,070 This line has not executed yet. 107 00:04:20,070 --> 00:04:21,720 So if we want to execute it, 108 00:04:21,720 --> 00:04:24,390 we can come over here and step over it or step into it. 109 00:04:24,390 --> 00:04:26,640 And I'll talk about all this in the course. 110 00:04:26,640 --> 00:04:28,830 And in this case, I wanna step over it 111 00:04:28,830 --> 00:04:31,290 and notice the output came out down here. 112 00:04:31,290 --> 00:04:32,790 So that's the line we're at. 113 00:04:32,790 --> 00:04:34,410 So I'm gonna step over it again 114 00:04:34,410 --> 00:04:36,570 and now we're gonna get the input from the user. 115 00:04:36,570 --> 00:04:38,373 So let's say I type in 1,200. 116 00:04:39,420 --> 00:04:41,520 Now as soon as I press Enter, 117 00:04:41,520 --> 00:04:44,640 that 1,200 will be read into num, right? 118 00:04:44,640 --> 00:04:46,710 So my variables should change. 119 00:04:46,710 --> 00:04:48,480 I'm gonna press Enter 120 00:04:48,480 --> 00:04:50,790 and now notice that num up here is 1,200. 121 00:04:50,790 --> 00:04:53,040 I can also hover over num over here and it's 1,200. 122 00:04:53,040 --> 00:04:56,370 So this is a really, really powerful feature. 123 00:04:56,370 --> 00:04:59,430 Debuggers are great. We use them all the time. 124 00:04:59,430 --> 00:05:00,330 We don't overuse them. 125 00:05:00,330 --> 00:05:03,330 If it's a really simple program and you've got simple logic 126 00:05:03,330 --> 00:05:05,760 and you can test your code easily, 127 00:05:05,760 --> 00:05:07,290 there's no real need to debug it, 128 00:05:07,290 --> 00:05:08,820 but sometimes it comes in really handy 129 00:05:08,820 --> 00:05:10,710 when we're trying to figure out a problem 130 00:05:10,710 --> 00:05:13,290 or looking at some really complex logic 131 00:05:13,290 --> 00:05:15,870 that we wanna walk through step by step. 132 00:05:15,870 --> 00:05:16,703 So that's it. 133 00:05:16,703 --> 00:05:18,120 Now, if we wanna stop the program, 134 00:05:18,120 --> 00:05:22,290 we could just click that little red box right here to stop. 135 00:05:22,290 --> 00:05:24,510 If we want to just continue full blast 136 00:05:24,510 --> 00:05:26,070 and skip all the break points, 137 00:05:26,070 --> 00:05:28,770 we can click that little arrow right here 138 00:05:28,770 --> 00:05:31,050 or we could just click on it a few times 139 00:05:31,050 --> 00:05:32,610 until the program ends. 140 00:05:32,610 --> 00:05:34,860 And let me just stop it here 141 00:05:34,860 --> 00:05:35,880 so you can see what that looks like. 142 00:05:35,880 --> 00:05:36,783 So I stop it. 143 00:05:38,310 --> 00:05:41,550 So now it's gone, notice it's changed back to blue down here 144 00:05:41,550 --> 00:05:44,850 and I can come back to my Explorer 145 00:05:44,850 --> 00:05:46,530 and I can see everything that I'm doing. 146 00:05:46,530 --> 00:05:49,260 So there you go, that's how the debugger works. 147 00:05:49,260 --> 00:05:51,150 In the next video, what I'm going to do 148 00:05:51,150 --> 00:05:53,730 is I'm gonna show you how to use Visual Studio Code 149 00:05:53,730 --> 00:05:56,940 with the source code that I've provided for the course. 150 00:05:56,940 --> 00:05:59,460 The source code that I provided for the course 151 00:05:59,460 --> 00:06:03,030 is in the format of CodeLite projects, but that's okay. 152 00:06:03,030 --> 00:06:05,160 They'll work in Visual Studio Code as well, 153 00:06:05,160 --> 00:06:07,973 and I'll show you exactly how to use them in the next video.