1 00:00:00,000 --> 00:00:02,499 (upbeat music) 2 00:00:02,499 --> 00:00:04,800 (keyboard types) 3 00:00:04,800 --> 00:00:05,633 Frank: Welcome back. 4 00:00:05,633 --> 00:00:07,290 In this video, I'm going to show you how 5 00:00:07,290 --> 00:00:11,670 to set up the debugger so that we can debug C++ programs 6 00:00:11,670 --> 00:00:13,440 in Visual Studio Code. 7 00:00:13,440 --> 00:00:15,690 The debugger is a great tool that allows us 8 00:00:15,690 --> 00:00:19,170 to execute our code one line at a time. 9 00:00:19,170 --> 00:00:21,393 We can inspect variables as we do that. 10 00:00:22,410 --> 00:00:24,450 Let's use the same project that we wrote 11 00:00:24,450 --> 00:00:27,990 in the previous video, and I'm gonna debug project two here. 12 00:00:27,990 --> 00:00:29,460 I'm just gonna make one little change to it. 13 00:00:29,460 --> 00:00:32,430 I'm gonna initialize favorite number to a 100 14 00:00:32,430 --> 00:00:34,983 just so we can see it, and see it when it changes. 15 00:00:36,000 --> 00:00:38,310 All right, so where do we start? Well, it's pretty easy. 16 00:00:38,310 --> 00:00:41,850 You just select any main.cpp file. 17 00:00:41,850 --> 00:00:45,128 What we're doing now is we're configuring the debugger. 18 00:00:45,128 --> 00:00:48,870 I'm gonna select main.cpp, then I'm going to go up to Run, 19 00:00:48,870 --> 00:00:50,433 and add a configuration. 20 00:00:51,900 --> 00:00:56,900 I'm going to select C++ GDB and my g++ compiler again. 21 00:00:58,410 --> 00:01:01,800 Now, you'll see this little menu bar pop up. 22 00:01:01,800 --> 00:01:05,640 Just click that little red box to stop the debugger 23 00:01:05,640 --> 00:01:07,350 and we'll finish configuring it. 24 00:01:07,350 --> 00:01:08,730 I'm going to close this window down here 25 00:01:08,730 --> 00:01:10,050 to give us some more room. 26 00:01:10,050 --> 00:01:12,930 You can see that a new file was created. 27 00:01:12,930 --> 00:01:15,240 It's called launch.json. 28 00:01:15,240 --> 00:01:17,640 This is the one that we're gonna modify slightly 29 00:01:17,640 --> 00:01:18,990 to get our debugger to work. 30 00:01:18,990 --> 00:01:20,610 The most important thing to do here is 31 00:01:20,610 --> 00:01:23,970 we want to change line 9, which is the type, 32 00:01:23,970 --> 00:01:28,590 and we want to change that from cppdbg to lldb. 33 00:01:28,590 --> 00:01:29,580 Now, when we do that, 34 00:01:29,580 --> 00:01:31,680 you'll see that we have some errors pop up here 35 00:01:31,680 --> 00:01:34,320 on lines 13, 15, 16, and 17. 36 00:01:34,320 --> 00:01:36,573 We want to delete those lines completely. 37 00:01:37,860 --> 00:01:39,063 Let me get rid of that. 38 00:01:40,680 --> 00:01:43,290 We'll get rid of these three lines as well. 39 00:01:43,290 --> 00:01:45,897 That's what we've got now on line 13. 40 00:01:45,897 --> 00:01:47,640 Cwd should be fileDirname. 41 00:01:47,640 --> 00:01:49,680 If it's anything other than that, be sure to change it 42 00:01:49,680 --> 00:01:52,740 to fileDirname, and that's it, we're done. 43 00:01:52,740 --> 00:01:54,120 Again, make sure there's no squigglies 44 00:01:54,120 --> 00:01:55,590 and no errors in here. 45 00:01:55,590 --> 00:01:56,820 I just save that 46 00:01:56,820 --> 00:01:59,190 and I'm going to close it because we're done with it. 47 00:01:59,190 --> 00:02:00,780 Now, we're ready to debug. 48 00:02:00,780 --> 00:02:02,040 To start the debugger, 49 00:02:02,040 --> 00:02:04,890 select the file that you want to debug. 50 00:02:04,890 --> 00:02:08,580 In this case, I want to debug main.cpp in project two. 51 00:02:08,580 --> 00:02:10,710 If I wanted to debug project one, 52 00:02:10,710 --> 00:02:12,963 I would click on main.cpp in project one. 53 00:02:14,250 --> 00:02:17,700 I want project two's main.cpp, so I've just selected it 54 00:02:17,700 --> 00:02:19,230 Here it is in my editor. 55 00:02:19,230 --> 00:02:20,670 To start the debugger, 56 00:02:20,670 --> 00:02:23,580 you click on this bug icon right here, Run and Debug. 57 00:02:23,580 --> 00:02:25,230 I'll do that now. 58 00:02:25,230 --> 00:02:27,150 You'll notice a couple of things happen. 59 00:02:27,150 --> 00:02:30,690 Well, one is you have this play arrow up here, 60 00:02:30,690 --> 00:02:33,720 and you have this information area where you have variables, 61 00:02:33,720 --> 00:02:36,360 and watches, and call stacks, and breakpoints. 62 00:02:36,360 --> 00:02:38,370 I'll cover these during the course. 63 00:02:38,370 --> 00:02:40,170 I'll cover them in CodeLite in the course, 64 00:02:40,170 --> 00:02:42,930 but it's the same concept in Visual Studio Code. 65 00:02:42,930 --> 00:02:45,210 What we need to do first is set a breakpoint. 66 00:02:45,210 --> 00:02:47,820 This is the place where the debugger is going 67 00:02:47,820 --> 00:02:49,980 to stop executing our code. 68 00:02:49,980 --> 00:02:52,290 You can do that right over here on the left side. 69 00:02:52,290 --> 00:02:54,540 If you just hover over this left gutter here. 70 00:02:54,540 --> 00:02:57,000 You see that red dot that keeps popping up? 71 00:02:57,000 --> 00:02:58,770 Well, you could just select that. 72 00:02:58,770 --> 00:03:02,670 Let's say that I wanted to stop right on that line. 73 00:03:02,670 --> 00:03:04,290 Now when the debugger starts executing, 74 00:03:04,290 --> 00:03:06,690 it's gonna run the program and stop here, 75 00:03:06,690 --> 00:03:08,820 and then we can control it from there. 76 00:03:08,820 --> 00:03:11,010 Also, notice that when I select that breakpoint, 77 00:03:11,010 --> 00:03:12,453 it shows up down here. 78 00:03:13,290 --> 00:03:15,960 Right, main.cpp's breakpoint in project two. 79 00:03:15,960 --> 00:03:18,180 Now, Visual Studio Code has a glitch 80 00:03:18,180 --> 00:03:20,250 where sometimes all the breakpoints 81 00:03:20,250 --> 00:03:22,140 and all the projects show up here. 82 00:03:22,140 --> 00:03:24,570 My recommendation is when you start your debugging, 83 00:03:24,570 --> 00:03:26,460 as soon as you click on that bug icon, 84 00:03:26,460 --> 00:03:28,950 just come down here and click that X 85 00:03:28,950 --> 00:03:30,030 if there's anything there. 86 00:03:30,030 --> 00:03:32,370 That clears out all the breakpoints everywhere. 87 00:03:32,370 --> 00:03:35,130 Then, you can just come back up and select your breakpoint, 88 00:03:35,130 --> 00:03:36,480 and you're good to go. 89 00:03:36,480 --> 00:03:38,880 Once you're ready to start debugging, we click 90 00:03:38,880 --> 00:03:42,300 on that green play button right up here on the top left. 91 00:03:42,300 --> 00:03:44,580 When I do that, you see this little menu option 92 00:03:44,580 --> 00:03:48,120 here comes up with these icons that control the debugger. 93 00:03:48,120 --> 00:03:50,670 The bottom bar down here turns orange. 94 00:03:50,670 --> 00:03:53,670 This area right here is where your variables show up. 95 00:03:53,670 --> 00:03:56,520 You can see my favorite number variable is a 100, 96 00:03:56,520 --> 00:03:58,890 and that's exactly what I initialized it to. 97 00:03:58,890 --> 00:04:02,610 We can also hover over our variable names just like that. 98 00:04:02,610 --> 00:04:04,050 You can see the 100 pop up. 99 00:04:04,050 --> 00:04:06,780 That's the value and that's the current value. 100 00:04:06,780 --> 00:04:09,273 If we change it, that number's going to change. 101 00:04:10,950 --> 00:04:13,440 You can see that line six executed right down here 102 00:04:13,440 --> 00:04:15,810 in the terminal: Hello from project two. 103 00:04:15,810 --> 00:04:18,240 Now, what I can do is I can click these buttons, 104 00:04:18,240 --> 00:04:20,070 and I'll explain these buttons during the course, 105 00:04:20,070 --> 00:04:22,140 but this one right here just steps over the code. 106 00:04:22,140 --> 00:04:23,910 It executes that statement. 107 00:04:23,910 --> 00:04:25,890 If I want to execute that statement, 108 00:04:25,890 --> 00:04:28,350 now, the program is basically waiting 109 00:04:28,350 --> 00:04:29,880 for us to type something in, right? 110 00:04:29,880 --> 00:04:32,160 Right here, we have to enter our favorite number. 111 00:04:32,160 --> 00:04:34,050 I'm gonna enter in something other than 100 112 00:04:34,050 --> 00:04:36,390 so that we can see favorite number change. 113 00:04:36,390 --> 00:04:39,690 Let me type in 45 and press Enter. 114 00:04:39,690 --> 00:04:42,690 You can see now that the playhead is here on line nine. 115 00:04:42,690 --> 00:04:45,420 Line nine has not executed yet, 116 00:04:45,420 --> 00:04:47,520 but also I noticed I entered 45, 117 00:04:47,520 --> 00:04:48,630 but look what happened over here 118 00:04:48,630 --> 00:04:52,680 on the top left in my variables: favorite number is now 45. 119 00:04:52,680 --> 00:04:55,170 Again, I can hover over at any word that it's being used 120 00:04:55,170 --> 00:04:56,220 to see its value. 121 00:04:56,220 --> 00:04:57,240 This is really handy. 122 00:04:57,240 --> 00:04:59,730 Sometimes programming gets really complex. 123 00:04:59,730 --> 00:05:02,970 It's very helpful to be able to execute your code one line 124 00:05:02,970 --> 00:05:04,950 at a time, and see your variables, 125 00:05:04,950 --> 00:05:07,620 see the results of your calculations visually. 126 00:05:07,620 --> 00:05:09,450 It can be very, very useful. 127 00:05:09,450 --> 00:05:13,050 When we're done, we can just continue the program 128 00:05:13,050 --> 00:05:15,120 or we can stop the program right here. 129 00:05:15,120 --> 00:05:17,340 In this case, I'm just going to continue. 130 00:05:17,340 --> 00:05:20,520 You can see it says here, "Your favorite number is 45." 131 00:05:20,520 --> 00:05:24,090 At this point, we're done. This bottom bar turns blue again. 132 00:05:24,090 --> 00:05:26,460 If we want to continue editing and so forth, 133 00:05:26,460 --> 00:05:30,570 we can just click Explorer and go right back to our files. 134 00:05:30,570 --> 00:05:31,680 So that's it. 135 00:05:31,680 --> 00:05:34,050 At this point, we've set up Visual Studio Code 136 00:05:34,050 --> 00:05:35,820 for building a task. 137 00:05:35,820 --> 00:05:38,160 We know how to execute our executable, 138 00:05:38,160 --> 00:05:40,230 and we know how to debug our project. 139 00:05:40,230 --> 00:05:41,063 In the next video, 140 00:05:41,063 --> 00:05:43,440 I'll show you how we can use the source code 141 00:05:43,440 --> 00:05:46,500 that I provide for the course within Visual Studio Code. 142 00:05:46,500 --> 00:05:47,450 I'll see you there.