1 00:00:00,000 --> 00:00:02,550 (upbeat music) 2 00:00:02,550 --> 00:00:05,058 (keyboard typing) 3 00:00:05,058 --> 00:00:06,530 -: [Frank Mitropoulos] Hello, everyone. 4 00:00:06,530 --> 00:00:08,581 In this video, we'll set up Visual Studio Code 5 00:00:08,581 --> 00:00:11,349 to debug C++ programs. 6 00:00:11,349 --> 00:00:13,410 We've already set up IntelliSense 7 00:00:13,410 --> 00:00:15,600 and we've set up a build task. 8 00:00:15,600 --> 00:00:17,280 So, now we're gonna set up the debug. 9 00:00:17,280 --> 00:00:18,270 Where do we start? 10 00:00:18,270 --> 00:00:19,590 It's really quite simple. 11 00:00:19,590 --> 00:00:23,430 Select one of your C++ files, it doesn't matter which one. 12 00:00:23,430 --> 00:00:27,930 And then, come up here to run and add configuration. 13 00:00:27,930 --> 00:00:30,240 Now, you're gonna be asked for your environment, 14 00:00:30,240 --> 00:00:35,190 select C++ GDB/LLDB, and then the C++ compiler again, 15 00:00:35,190 --> 00:00:37,423 which in our case is G++. 16 00:00:38,970 --> 00:00:40,380 Now, you're gonna have some stuff happen 17 00:00:40,380 --> 00:00:42,420 and things are gonna be changing colors and so forth. 18 00:00:42,420 --> 00:00:44,070 Don't worry about any of that. 19 00:00:44,070 --> 00:00:46,590 What we're gonna do now is modify this file 20 00:00:46,590 --> 00:00:48,930 that was just created called launch.json. 21 00:00:48,930 --> 00:00:51,300 And we're gonna make one minor change to it. 22 00:00:51,300 --> 00:00:53,490 Let me close this window up so we can see it. 23 00:00:53,490 --> 00:00:56,010 What we're going to do is we're going to change 24 00:00:56,010 --> 00:00:58,200 the entry for cwd. 25 00:00:58,200 --> 00:01:00,600 It's on line 14 on my system here, 26 00:01:00,600 --> 00:01:02,790 and we're gonna change this from workspace folder 27 00:01:02,790 --> 00:01:04,110 to file your name. 28 00:01:04,110 --> 00:01:08,223 So, I'm just gonna copy that and paste it in here. 29 00:01:10,290 --> 00:01:11,640 And that's all we're gonna do. 30 00:01:11,640 --> 00:01:14,580 Now, I'm gonna save this and I'm going to close it 31 00:01:14,580 --> 00:01:15,960 since we don't need it any longer. 32 00:01:15,960 --> 00:01:18,540 All right, let's debug our first program. 33 00:01:18,540 --> 00:01:22,590 Let's modify project two's main CPP file, just a little 34 00:01:22,590 --> 00:01:25,050 so we have a little information to see in the debugger. 35 00:01:25,050 --> 00:01:27,810 So, what I'm going to do here, I'm going to add an integer 36 00:01:27,810 --> 00:01:30,660 and let's just call this my favorite number 37 00:01:30,660 --> 00:01:32,312 and I'm gonna initialize it to, let's say a hundred, 38 00:01:32,312 --> 00:01:34,320 just so we have something to look at. 39 00:01:34,320 --> 00:01:36,750 So, it's still going to display hello from project two, 40 00:01:36,750 --> 00:01:38,280 but now we want it to prompt the user 41 00:01:38,280 --> 00:01:39,840 to enter their favorite number. 42 00:01:39,840 --> 00:01:41,550 So, we'll put another display statement in here 43 00:01:41,550 --> 00:01:43,980 and we'll just say, enter your favorite number, 44 00:01:43,980 --> 00:01:46,143 and then we'll read in what they type. 45 00:01:47,100 --> 00:01:49,590 And we're gonna read that into favorite number. 46 00:01:49,590 --> 00:01:51,690 And then, finally, we're gonna display 47 00:01:51,690 --> 00:01:53,820 your favorite number is, and we'll display 48 00:01:53,820 --> 00:01:55,980 what they typed in right back at them. 49 00:01:55,980 --> 00:01:58,290 So, we'll say your favorite number is, 50 00:01:58,290 --> 00:01:59,760 and we'll display what they entered. 51 00:01:59,760 --> 00:02:02,190 Fave number, followed by a new line. 52 00:02:02,190 --> 00:02:03,023 All right, so that's it. 53 00:02:03,023 --> 00:02:05,460 So, we made a small change, but an important change 54 00:02:05,460 --> 00:02:06,600 because we wanna be able to see 55 00:02:06,600 --> 00:02:09,300 that favorite number change in the debugger. 56 00:02:09,300 --> 00:02:11,160 All right, so how do we debug now? 57 00:02:11,160 --> 00:02:13,830 Select the program you want to debug, again, 58 00:02:13,830 --> 00:02:15,990 very important that we select that CPP file 59 00:02:15,990 --> 00:02:16,860 that we want to debug. 60 00:02:16,860 --> 00:02:19,560 In this case, this is the one we want to debug. 61 00:02:19,560 --> 00:02:22,320 What we can do now is we can click that bug icon 62 00:02:22,320 --> 00:02:24,270 right there where it says, run a debug, 63 00:02:25,290 --> 00:02:27,000 and nothing will happen right away. 64 00:02:27,000 --> 00:02:27,990 What we need to do is we need 65 00:02:27,990 --> 00:02:29,999 to set a break point in our code. 66 00:02:29,999 --> 00:02:33,420 This is where the debugger is going to stop 67 00:02:33,420 --> 00:02:35,130 when it starts to debug our code. 68 00:02:35,130 --> 00:02:38,280 So, I want it to stop right there, right on line five. 69 00:02:38,280 --> 00:02:40,080 So, you could just move along here 70 00:02:40,080 --> 00:02:41,430 and just select the break points. 71 00:02:41,430 --> 00:02:43,590 Just click and it turns red and you can select 72 00:02:43,590 --> 00:02:45,810 as many break points as you want. 73 00:02:45,810 --> 00:02:48,930 Notice now that the break points show up here. 74 00:02:48,930 --> 00:02:51,060 There is a glitch in Visual Studio Code 75 00:02:51,060 --> 00:02:53,550 where sometimes when you have multiple breakpoints 76 00:02:53,550 --> 00:02:56,220 across different projects, they all show here. 77 00:02:56,220 --> 00:02:58,470 And what I'd recommend you do is just when you're about 78 00:02:58,470 --> 00:03:01,380 to start debugging, click on that X right there. 79 00:03:01,380 --> 00:03:03,780 That erases all the break points from everywhere. 80 00:03:03,780 --> 00:03:05,130 Now, you don't see any down here, 81 00:03:05,130 --> 00:03:06,720 and then, you could just come up to your project 82 00:03:06,720 --> 00:03:09,900 and put the break point exactly where you want it. 83 00:03:09,900 --> 00:03:11,190 So, now that we've done that, 84 00:03:11,190 --> 00:03:15,003 we can click on this little green run icon right up here. 85 00:03:15,003 --> 00:03:17,760 And when I do that, the debugger will engage. 86 00:03:17,760 --> 00:03:19,380 You can see that there's a build happening 87 00:03:19,380 --> 00:03:22,200 and now this bottom bar turns orange 88 00:03:22,200 --> 00:03:24,285 and now there's a couple of things that have just happened. 89 00:03:24,285 --> 00:03:27,060 Notice that this line five is highlighted 90 00:03:27,060 --> 00:03:30,330 and the playhead is right there, right on line five. 91 00:03:30,330 --> 00:03:32,850 That tells you that the debugger stopped at line five, 92 00:03:32,850 --> 00:03:33,870 just like you asked it to. 93 00:03:33,870 --> 00:03:35,460 That's where you put the break point. 94 00:03:35,460 --> 00:03:39,660 So, line five has not been executed, line four has. 95 00:03:39,660 --> 00:03:43,440 Notice that line four is our declaration of favorite number. 96 00:03:43,440 --> 00:03:45,450 And if you look over here in your local variables, 97 00:03:45,450 --> 00:03:46,740 there, you see its value. 98 00:03:46,740 --> 00:03:47,940 That's pretty cool. 99 00:03:47,940 --> 00:03:49,500 So, you can see favorite number is a hundred. 100 00:03:49,500 --> 00:03:52,290 You can also hover over favorite number. 101 00:03:52,290 --> 00:03:53,520 You'll see the hundred pop up. 102 00:03:53,520 --> 00:03:55,560 You can do that anywhere it occurs. 103 00:03:55,560 --> 00:03:56,880 Now, what we want to do is we want 104 00:03:56,880 --> 00:03:58,470 to execute one line at a time. 105 00:03:58,470 --> 00:04:00,090 And we can do that with this button right here. 106 00:04:00,090 --> 00:04:02,280 You can see we've got a series of buttons here 107 00:04:02,280 --> 00:04:05,130 that allow us to interact with the debugger. 108 00:04:05,130 --> 00:04:07,290 This steps over the code, this steps into code. 109 00:04:07,290 --> 00:04:10,560 And I discuss these in more detail throughout the course. 110 00:04:10,560 --> 00:04:12,690 Here we can just stop the debugger 111 00:04:12,690 --> 00:04:14,310 and here we can just continue. 112 00:04:14,310 --> 00:04:16,980 So, what I wanna do is I want to execute one line at a time. 113 00:04:16,980 --> 00:04:18,990 So, I'm going to select step over. 114 00:04:18,990 --> 00:04:21,720 And you can see the playhead now moves to line six 115 00:04:21,720 --> 00:04:24,120 and line five executed and displayed 116 00:04:24,120 --> 00:04:26,970 something on the screen just like we expected. 117 00:04:26,970 --> 00:04:28,950 All right, and now it's gonna, 118 00:04:28,950 --> 00:04:30,300 it's asking you what's your favorite number. 119 00:04:30,300 --> 00:04:31,560 So, I'm just gonna type in something 120 00:04:31,560 --> 00:04:33,480 other than a hundred so you can see it change. 121 00:04:33,480 --> 00:04:36,130 Let's just type in 45, and I'll press enter 122 00:04:36,990 --> 00:04:40,050 and notice my local variable right here, 45. 123 00:04:40,050 --> 00:04:41,340 That's pretty neat. 124 00:04:41,340 --> 00:04:43,920 This allows me to step through my code 125 00:04:43,920 --> 00:04:45,240 and see what's happening. 126 00:04:45,240 --> 00:04:46,710 Maybe I made a mistake somewhere, 127 00:04:46,710 --> 00:04:49,050 it's much easier to see it this way. 128 00:04:49,050 --> 00:04:50,370 And then, we could just continue writing 129 00:04:50,370 --> 00:04:52,290 the program and we're done. 130 00:04:52,290 --> 00:04:53,790 So, you see hello from project two, 131 00:04:53,790 --> 00:04:54,750 enter your favorite number. 132 00:04:54,750 --> 00:04:56,693 I entered 45, and then it displayed 133 00:04:56,693 --> 00:05:00,030 your favorite number is 45, just as we expected. 134 00:05:00,030 --> 00:05:03,360 When you're done debugging, you can click over here, 135 00:05:03,360 --> 00:05:06,540 go back to explore, and just debug 136 00:05:06,540 --> 00:05:09,180 or code whatever project you like. 137 00:05:09,180 --> 00:05:11,670 All right, so that shows you how to set up debugging. 138 00:05:11,670 --> 00:05:14,370 So, at this point we've got building, we've got running, 139 00:05:14,370 --> 00:05:16,140 and we've got debugging all set up. 140 00:05:16,140 --> 00:05:17,640 In the next video, I'm gonna show you 141 00:05:17,640 --> 00:05:20,820 how to use Visual Studio Code with the source code 142 00:05:20,820 --> 00:05:22,503 that I provide for the course.