1 00:00:00,247 --> 00:00:02,159 (bright ascending music) 2 00:00:02,159 --> 00:00:02,992 (air whooshing) 3 00:00:02,992 --> 00:00:05,620 (keyboard typing) 4 00:00:05,620 --> 00:00:06,453 -: [Frank Mitropoulos] Welcome back. 5 00:00:06,453 --> 00:00:09,150 Now that we've got Visual Studio Code installed 6 00:00:09,150 --> 00:00:12,180 and the C++ extension installed, 7 00:00:12,180 --> 00:00:15,180 we can now start creating C++ projects 8 00:00:15,180 --> 00:00:18,000 that we can build and run within Visual Studio Code. 9 00:00:18,000 --> 00:00:21,240 So, first thing we need to do is we need to create a folder. 10 00:00:21,240 --> 00:00:23,667 I'm going to create the folder, not within VSCode, 11 00:00:23,667 --> 00:00:26,910 but I'm gonna create it right on my Desktop. 12 00:00:26,910 --> 00:00:28,560 So I'm just gonna come over here to my Desktop 13 00:00:28,560 --> 00:00:30,237 and I'm gonna create a New Folder, 14 00:00:30,237 --> 00:00:33,030 and I'm just gonna call it "SectionX." 15 00:00:33,030 --> 00:00:35,370 The idea being that whatever section you're working on, 16 00:00:35,370 --> 00:00:38,550 you can create a Section5, Section6, Section7 folder, 17 00:00:38,550 --> 00:00:39,720 and so forth. 18 00:00:39,720 --> 00:00:41,640 So this could represent any folder, 19 00:00:41,640 --> 00:00:45,660 we'll create it, and I'll close this up. 20 00:00:45,660 --> 00:00:48,030 Then, we want to go into Visual Studio Code 21 00:00:48,030 --> 00:00:50,520 and open that folder we just created. 22 00:00:50,520 --> 00:00:52,680 We can click the blue button here if this is open, 23 00:00:52,680 --> 00:00:56,220 we can also hit Control+O, Control+K, and so forth. 24 00:00:56,220 --> 00:01:01,220 Or we can come up here to File, Open Folder right there. 25 00:01:01,530 --> 00:01:04,560 So I'll just do that, and I'm going to select the folder 26 00:01:04,560 --> 00:01:07,170 I just created on my Desktop, "SectionX," 27 00:01:07,170 --> 00:01:08,940 and I'll press Okay. 28 00:01:08,940 --> 00:01:11,280 And now, you can see that that folder has been opened. 29 00:01:11,280 --> 00:01:12,930 It's right here, "SectionX.' 30 00:01:12,930 --> 00:01:15,510 There's nothing in it right now, it's just an empty folder. 31 00:01:15,510 --> 00:01:16,830 And this Getting Started page, 32 00:01:16,830 --> 00:01:19,830 I'm just gonna close that to get it outta the way. 33 00:01:19,830 --> 00:01:20,663 Perfect. 34 00:01:20,663 --> 00:01:21,496 So now, what we need to do 35 00:01:21,496 --> 00:01:23,790 is we need to create a folder within this folder. 36 00:01:23,790 --> 00:01:25,860 This is the top-level folder. 37 00:01:25,860 --> 00:01:29,310 So we can do that by selecting that icon right there, 38 00:01:29,310 --> 00:01:30,900 which is New Folder. 39 00:01:30,900 --> 00:01:33,180 And I'm just gonna call this "Project1." 40 00:01:33,180 --> 00:01:36,150 So this will be my "Project1" folder, 41 00:01:36,150 --> 00:01:39,180 the idea being that we can create multiple project folders 42 00:01:39,180 --> 00:01:41,043 within this "SectionX" folder. 43 00:01:42,300 --> 00:01:45,510 Now once we do that, we need to create a C++ program 44 00:01:45,510 --> 00:01:46,710 that we'll be able to run, 45 00:01:46,710 --> 00:01:48,690 and we wanna do that within "Project1." 46 00:01:48,690 --> 00:01:51,120 So we wanna be sure that "Project1" is selected, 47 00:01:51,120 --> 00:01:54,330 and I'm gonna press this icon right here for New File. 48 00:01:54,330 --> 00:01:59,330 And I want to create "main.cpp," that's our C++ program. 49 00:01:59,430 --> 00:02:02,100 When I select that, it opens up on this side here. 50 00:02:02,100 --> 00:02:03,443 And what I want to do here 51 00:02:03,443 --> 00:02:06,330 is I just wanna write a really simple C++ program, 52 00:02:06,330 --> 00:02:10,919 and I'm going to do "#include." 53 00:02:10,919 --> 00:02:12,300 A lotta this won't make sense to you 54 00:02:12,300 --> 00:02:13,440 if you're just starting the course. 55 00:02:13,440 --> 00:02:16,020 That's okay, just follow along, type along, 56 00:02:16,020 --> 00:02:19,260 and this will all be explained as the course progresses. 57 00:02:19,260 --> 00:02:22,110 Then we're gonna say, "using namespace standard." 58 00:02:22,110 --> 00:02:23,250 And now, what we're going to do 59 00:02:23,250 --> 00:02:25,350 is we're gonna create the main function. 60 00:02:25,350 --> 00:02:26,907 So we'll say, "int main," 61 00:02:28,050 --> 00:02:29,760 and we'll just do a display statement, 62 00:02:29,760 --> 00:02:30,630 something really simple. 63 00:02:30,630 --> 00:02:34,620 We'll just say "cout," and, "Hello from 'Project1.'" 64 00:02:34,620 --> 00:02:36,990 And we'll put a new line at the end. 65 00:02:36,990 --> 00:02:38,490 And finally, we'll "return 0." 66 00:02:38,490 --> 00:02:39,323 As I said, 67 00:02:39,323 --> 00:02:42,150 the syntax will be explained as the course progresses. 68 00:02:42,150 --> 00:02:43,170 Now, I'm gonna save that. 69 00:02:43,170 --> 00:02:47,250 So I'm just gonna come to here, File, Save, or Control+S. 70 00:02:47,250 --> 00:02:48,690 We can't run it quite yet, 71 00:02:48,690 --> 00:02:51,090 we've got to do a little bit of configuration 72 00:02:51,090 --> 00:02:53,970 which is gonna help us build and run this project. 73 00:02:53,970 --> 00:02:55,950 So there's two things we need to do. 74 00:02:55,950 --> 00:02:58,710 First, we need to configure IntelliSense. 75 00:02:58,710 --> 00:03:02,370 That's the piece of software that runs in Visual Studio Code 76 00:03:02,370 --> 00:03:04,080 that kinda watches you as you type, 77 00:03:04,080 --> 00:03:06,570 and it's tryna help you with ideas, and suggestions, 78 00:03:06,570 --> 00:03:08,340 and finding mistakes as you go. 79 00:03:08,340 --> 00:03:11,523 So we could go to View, Command Palette. 80 00:03:12,390 --> 00:03:13,830 And then what we wanted to do, right in here, 81 00:03:13,830 --> 00:03:16,140 we wanted to just type "C++," 82 00:03:16,140 --> 00:03:19,312 and find this Edit Configurations UI. 83 00:03:19,312 --> 00:03:21,480 C++, Edit Configurations UI. 84 00:03:21,480 --> 00:03:23,220 It might be somewhere down here. 85 00:03:23,220 --> 00:03:25,380 In this case, it showed up right up here for me. 86 00:03:25,380 --> 00:03:27,060 So I just wanna select that, 87 00:03:27,060 --> 00:03:29,550 and we're gonna make a couple of small changes to this. 88 00:03:29,550 --> 00:03:31,980 First of all, go to the "compiler path," 89 00:03:31,980 --> 00:03:36,170 and be sure that you don't have GCC, but we want G++ 90 00:03:37,230 --> 00:03:38,908 We want G++. 91 00:03:38,908 --> 00:03:41,880 We wanna use a C++ compiler, not the "C" compiler. 92 00:03:41,880 --> 00:03:43,770 So you wanna be sure that that's there. 93 00:03:43,770 --> 00:03:46,770 And then we wanna come down to the "C++ standard," 94 00:03:46,770 --> 00:03:49,320 and we want to use "C++17." 95 00:03:49,320 --> 00:03:50,153 There we go. 96 00:03:50,153 --> 00:03:50,986 That's it. 97 00:03:50,986 --> 00:03:53,730 So now, we can go File, Save that file, 98 00:03:53,730 --> 00:03:57,090 and we can close that up 'cause we're done with that. 99 00:03:57,090 --> 00:03:59,940 Now, you'll notice what happened over here on the left side, 100 00:03:59,940 --> 00:04:02,580 a ".vscode" folder was created. 101 00:04:02,580 --> 00:04:04,410 And that folder contains a file 102 00:04:04,410 --> 00:04:07,590 called "cpp_properties.json," 103 00:04:07,590 --> 00:04:10,710 and that file contains the configuration information 104 00:04:10,710 --> 00:04:13,110 for IntelliSense, what we just set up. 105 00:04:13,110 --> 00:04:13,943 Perfect. 106 00:04:13,943 --> 00:04:17,220 So the next step is to create a default build task. 107 00:04:17,220 --> 00:04:20,640 That's what's going to compile the code and build it. 108 00:04:20,640 --> 00:04:23,400 So in order to do that, we want to come to Terminal, 109 00:04:23,400 --> 00:04:26,100 and select Configure Default Build Task, 110 00:04:26,100 --> 00:04:27,330 right here at the bottom. 111 00:04:27,330 --> 00:04:28,567 And at this point, it's asking you, 112 00:04:28,567 --> 00:04:30,090 "Hey, what compiler are you using?" 113 00:04:30,090 --> 00:04:33,060 Be sure to select the G++ option, 114 00:04:33,060 --> 00:04:37,410 not CPP or anything else, just G++. 115 00:04:37,410 --> 00:04:39,600 You may have several different versions, like I do, 116 00:04:39,600 --> 00:04:43,020 just select G++, and it will use the most recent version. 117 00:04:43,020 --> 00:04:43,853 So I'll do that. 118 00:04:43,853 --> 00:04:45,930 And now, I get this file that pops up. 119 00:04:45,930 --> 00:04:48,510 We need to make a few small changes to this file 120 00:04:48,510 --> 00:04:50,760 so we can configure the build. 121 00:04:50,760 --> 00:04:54,120 I'm coming here to line nine in the "args" section. 122 00:04:54,120 --> 00:04:56,670 And I just wanna add, right after the "-g," 123 00:04:56,670 --> 00:05:01,670 I wanna add minus or dash capital "W," "all," 124 00:05:02,130 --> 00:05:05,640 and the "all" is in lowercase, followed by a comma. 125 00:05:05,640 --> 00:05:09,150 That tells the build to display all warnings. 126 00:05:09,150 --> 00:05:14,150 Then we need to do "-std=c++17," 127 00:05:14,700 --> 00:05:16,710 with a comma at the end again. 128 00:05:16,710 --> 00:05:20,493 That tells the build system to use the C++17 standard. 129 00:05:21,330 --> 00:05:24,060 And we've got one more small change right here. 130 00:05:24,060 --> 00:05:29,060 We want to change line 12 from "file" to "fileDirname." 131 00:05:30,030 --> 00:05:32,790 So what I wanna do is I want to copy that 132 00:05:32,790 --> 00:05:36,930 and paste it right in here, just like that. 133 00:05:36,930 --> 00:05:38,520 So now, we've got "fileDirname." 134 00:05:38,520 --> 00:05:40,440 We still have the dollar sign and the curlies, 135 00:05:40,440 --> 00:05:42,957 but we want to change "file" to "fileDirname." 136 00:05:44,550 --> 00:05:45,750 So the next thing we want to do 137 00:05:45,750 --> 00:05:50,750 is we want to add, "/*.cpp" right after that closed curly. 138 00:05:53,430 --> 00:05:55,530 This tells the build system 139 00:05:55,530 --> 00:06:00,120 to build all the C++ files in that project. 140 00:06:00,120 --> 00:06:04,200 By default, Visual Studio Code, when it comes configured, 141 00:06:04,200 --> 00:06:06,390 it only does the one file. 142 00:06:06,390 --> 00:06:09,030 So as we build projects that get bigger, 143 00:06:09,030 --> 00:06:11,340 we're gonna have multiple C++ files. 144 00:06:11,340 --> 00:06:13,860 So, that's really important that we do that. 145 00:06:13,860 --> 00:06:16,230 So I'm gonna Control+S to save, 146 00:06:16,230 --> 00:06:18,540 and I'm going to close that down. 147 00:06:18,540 --> 00:06:22,290 And now, we're ready to build and run this program. 148 00:06:22,290 --> 00:06:23,820 So, what do we do first? 149 00:06:23,820 --> 00:06:27,240 It's very important that we select the C++ file 150 00:06:27,240 --> 00:06:29,610 that we want to build within the right project. 151 00:06:29,610 --> 00:06:32,550 It's possible that we'll have multiple projects, 152 00:06:32,550 --> 00:06:36,810 and every project will have its own C++ file. 153 00:06:36,810 --> 00:06:38,190 That has to be selected 154 00:06:38,190 --> 00:06:41,100 because the build system in Visual Studio Code 155 00:06:41,100 --> 00:06:42,720 will build the active file, 156 00:06:42,720 --> 00:06:44,790 which is the one that's been selected. 157 00:06:44,790 --> 00:06:48,483 So once we have that selected, we can come up to Terminal, 158 00:06:49,440 --> 00:06:52,443 and Run Build Task or Control+Shift+B. 159 00:06:53,610 --> 00:06:55,530 And what'll happen is right down here, 160 00:06:55,530 --> 00:06:58,080 you can see the output of the build, 161 00:06:58,080 --> 00:07:01,020 it says, "Build finished successfully." 162 00:07:01,020 --> 00:07:04,650 You can also see our G++ compiler being executed 163 00:07:04,650 --> 00:07:08,310 with those options that we typed into our test JSON. 164 00:07:08,310 --> 00:07:12,750 And you can also see that it's using the "Project1" folder, 165 00:07:12,750 --> 00:07:15,540 and it's compiling all the "*.cpp" files. 166 00:07:15,540 --> 00:07:18,663 So it's doing exactly what we told it to do. 167 00:07:19,500 --> 00:07:23,130 So now, we have an executable that we can run, 168 00:07:23,130 --> 00:07:25,050 and that executable is called "main," 169 00:07:25,050 --> 00:07:27,270 and it's right here in the project. 170 00:07:27,270 --> 00:07:29,190 Before we only had the one source file, 171 00:07:29,190 --> 00:07:31,260 now we've got the executable file. 172 00:07:31,260 --> 00:07:32,370 So, how do we run that? 173 00:07:32,370 --> 00:07:34,980 Lemme close this up and just make a little bit more room. 174 00:07:34,980 --> 00:07:36,993 We can right click on that file, 175 00:07:38,340 --> 00:07:41,250 and select Open an Integrated Terminal. 176 00:07:41,250 --> 00:07:43,650 That will open up a terminal window down here, 177 00:07:43,650 --> 00:07:45,660 and notice it's in the "Project1" folder. 178 00:07:45,660 --> 00:07:47,550 That's exactly where we want to be. 179 00:07:47,550 --> 00:07:48,840 And if you type "ls," 180 00:07:48,840 --> 00:07:51,600 that will list the files in that directory. 181 00:07:51,600 --> 00:07:54,300 And you can see there's my "main.cpp," 182 00:07:54,300 --> 00:07:56,820 and there's my main executable, it's green. 183 00:07:56,820 --> 00:07:58,590 So if I want to run this program, 184 00:07:58,590 --> 00:08:02,820 all I need to do is type "./main." 185 00:08:02,820 --> 00:08:06,840 Really important that you use the "./" so it can find it. 186 00:08:06,840 --> 00:08:07,920 And when we press Enter, 187 00:08:07,920 --> 00:08:10,500 we expect this to display, "Hello from 'Project1.'" 188 00:08:10,500 --> 00:08:11,403 So, let's do it. 189 00:08:12,360 --> 00:08:15,000 And that's exactly what we get, "Hello from 'Project1.'" 190 00:08:15,000 --> 00:08:16,350 So there you go. 191 00:08:16,350 --> 00:08:18,510 This shows you how to set this up 192 00:08:18,510 --> 00:08:22,140 so that it can build and run C++ projects. 193 00:08:22,140 --> 00:08:24,270 Right now, we only have the one project, "Project1," 194 00:08:24,270 --> 00:08:26,550 so let's create another project. 195 00:08:26,550 --> 00:08:28,170 We need to create a new folder. 196 00:08:28,170 --> 00:08:30,930 The problem is if we create a new folder right now, 197 00:08:30,930 --> 00:08:33,570 it's going to create it underneath "Project1." 198 00:08:33,570 --> 00:08:34,890 That's not what I want. 199 00:08:34,890 --> 00:08:39,030 I want "Project2" to be at the same level as "Project1." 200 00:08:39,030 --> 00:08:41,400 And it's not really intuitive as to how you would do that. 201 00:08:41,400 --> 00:08:42,990 You would think, "You select this one," 202 00:08:42,990 --> 00:08:44,550 but then the folders go away. 203 00:08:44,550 --> 00:08:47,490 So the way to do it is just select "Project1," 204 00:08:47,490 --> 00:08:49,590 and then press Escape. 205 00:08:49,590 --> 00:08:51,450 And when you press Escape, notice what happens, 206 00:08:51,450 --> 00:08:54,510 you get this blue bounding box here goin' around. 207 00:08:54,510 --> 00:08:56,400 Now when you create the folder, 208 00:08:56,400 --> 00:08:59,190 you can type in "Project2," or whatever you like, 209 00:08:59,190 --> 00:09:01,620 and it's at the same level as "Project1." 210 00:09:01,620 --> 00:09:03,690 That's exactly how you would do that. 211 00:09:03,690 --> 00:09:07,290 We can create as many project folders as we need. 212 00:09:07,290 --> 00:09:10,890 Now in "Project2," I need to create my C++ file, 213 00:09:10,890 --> 00:09:14,787 so I'll select New File, and type in "main.cpp." 214 00:09:15,870 --> 00:09:18,750 And what I wanna put in here is... 215 00:09:18,750 --> 00:09:20,340 Let's put in the same thing that we had here. 216 00:09:20,340 --> 00:09:21,780 I'm just gonna copy and paste this, 217 00:09:21,780 --> 00:09:24,120 so that I don't have to type it all in again. 218 00:09:24,120 --> 00:09:27,330 So I'm just gonna select it all, and I'm going to Copy, 219 00:09:27,330 --> 00:09:29,490 and I'm gonna come back to "Project2's" main, 220 00:09:29,490 --> 00:09:30,663 and Paste it in here. 221 00:09:31,500 --> 00:09:32,340 There we go. 222 00:09:32,340 --> 00:09:33,930 Lemme close this window up down here, 223 00:09:33,930 --> 00:09:35,250 give us a little bit more room. 224 00:09:35,250 --> 00:09:38,450 And all I'm gonna do here is say, "Hello from 'Project2.'" 225 00:09:39,510 --> 00:09:41,643 And I'll Control+S to save this. 226 00:09:42,480 --> 00:09:47,067 So now if I want to build this project, "Project2," 227 00:09:48,024 --> 00:09:49,124 I have to select that. 228 00:09:50,010 --> 00:09:52,440 If you select this or this, and try to do a build, 229 00:09:52,440 --> 00:09:54,540 you're gonna get all kinds of weird things happening 230 00:09:54,540 --> 00:09:56,400 because it doesn't know what to do. 231 00:09:56,400 --> 00:10:00,600 So we wanna select the C++ file within "Project2," 232 00:10:00,600 --> 00:10:02,970 and again we wanna go to Terminal 233 00:10:02,970 --> 00:10:05,313 and we want to Run the Build Task. 234 00:10:06,510 --> 00:10:08,857 And notice, we get exactly the same thing, 235 00:10:08,857 --> 00:10:10,080 "Build finished successfully," 236 00:10:10,080 --> 00:10:13,710 except you can see it's working on "Project2" now. 237 00:10:13,710 --> 00:10:16,737 And we created an executable file in "Project2," 238 00:10:17,857 --> 00:10:19,710 "Project1" was not affected. 239 00:10:19,710 --> 00:10:24,090 So if we want to run this file, we can right click on it, 240 00:10:24,090 --> 00:10:25,983 open it in the integrated terminal. 241 00:10:26,880 --> 00:10:28,680 Notice we're in "Project2" folder now, 242 00:10:28,680 --> 00:10:31,290 and we can just do "./main." 243 00:10:31,290 --> 00:10:34,200 And we expect now to see, "Hello from 'Project2,'" 244 00:10:34,200 --> 00:10:35,750 and that's exactly what we get. 245 00:10:36,750 --> 00:10:37,583 So, that's it. 246 00:10:37,583 --> 00:10:40,380 That shows you how to create folder structures, 247 00:10:40,380 --> 00:10:44,370 create C++ files, build and run the active tasks. 248 00:10:44,370 --> 00:10:48,300 We can add as many projects as we like inside this folder, 249 00:10:48,300 --> 00:10:50,070 this "SectionX" folder. 250 00:10:50,070 --> 00:10:53,190 And it's a real handy way to keep yourself organized. 251 00:10:53,190 --> 00:10:54,600 In the next video, what we'll do 252 00:10:54,600 --> 00:10:57,900 is I'll show you how to set up Visual Studio Code 253 00:10:57,900 --> 00:11:00,930 so that we can debug C++ projects. 254 00:11:00,930 --> 00:11:02,580 Debugging is really important, 255 00:11:02,580 --> 00:11:05,490 and helps us find errors and understand our code better. 256 00:11:05,490 --> 00:11:06,750 And the idea with the debugger 257 00:11:06,750 --> 00:11:09,780 is that we can execute our code one line at a time, 258 00:11:09,780 --> 00:11:12,630 and we can jump around between pieces of code, 259 00:11:12,630 --> 00:11:15,390 and we can actually see the values of our variables 260 00:11:15,390 --> 00:11:17,130 as we're executing the code. 261 00:11:17,130 --> 00:11:20,880 So it's a very, very powerful tool to have in our tool belt. 262 00:11:20,880 --> 00:11:23,273 So, I'll see you in the next video and we'll do that.