1 00:00:00,000 --> 00:00:02,583 (bright music) 2 00:00:05,430 --> 00:00:06,510 Frank: Welcome back. 3 00:00:06,510 --> 00:00:09,300 Now that we've got Visual Studio Code installed 4 00:00:09,300 --> 00:00:11,160 and the extensions installed, 5 00:00:11,160 --> 00:00:14,400 what we're going to do is create a folder structure 6 00:00:14,400 --> 00:00:16,920 so that we can create different projects 7 00:00:16,920 --> 00:00:18,990 all within the same root folder. 8 00:00:18,990 --> 00:00:22,050 That's just gonna make it very efficient for us to work. 9 00:00:22,050 --> 00:00:25,260 So I've already created a folder and let me show it to you. 10 00:00:25,260 --> 00:00:26,329 It's right on my desktop 11 00:00:26,329 --> 00:00:29,670 and I created this folder called SectionX. 12 00:00:29,670 --> 00:00:31,770 The idea being if you're working on section one 13 00:00:31,770 --> 00:00:33,330 or two or three of the course, 14 00:00:33,330 --> 00:00:34,632 you can create your own section one, 15 00:00:34,632 --> 00:00:37,080 section two and section three folders. 16 00:00:37,080 --> 00:00:39,210 So this could represent any one of them. 17 00:00:39,210 --> 00:00:41,700 It's an empty folder, there's nothing in there right now. 18 00:00:41,700 --> 00:00:44,760 So what I want to do is, I want to open that folder 19 00:00:44,760 --> 00:00:46,380 in Visual Studio Code. 20 00:00:46,380 --> 00:00:48,840 So I can choose open folder or open 21 00:00:48,840 --> 00:00:50,280 or I could go to file open. 22 00:00:50,280 --> 00:00:52,140 So I'm just gonna click open folder 23 00:00:52,140 --> 00:00:56,580 and go to my desktop and select that SectionX folder 24 00:00:56,580 --> 00:00:57,690 and I'm going to open it. 25 00:00:57,690 --> 00:00:59,310 So now it's gonna open up in Xcode 26 00:00:59,310 --> 00:01:01,080 and you can see right here, there's the folder, 27 00:01:01,080 --> 00:01:02,640 there's nothing inside of it. 28 00:01:02,640 --> 00:01:04,650 Let me just make this window a little bit wider 29 00:01:04,650 --> 00:01:06,600 so we can see it a little bit better. 30 00:01:06,600 --> 00:01:08,250 So the idea is now that I want 31 00:01:08,250 --> 00:01:10,020 to create sub-folders in there 32 00:01:10,020 --> 00:01:12,540 and each one of those sub-folders corresponds 33 00:01:12,540 --> 00:01:14,340 to a different project. 34 00:01:14,340 --> 00:01:15,810 So the way we do that is, 35 00:01:15,810 --> 00:01:17,730 we can click this icon right here, 36 00:01:17,730 --> 00:01:19,980 that's the new folder icon, 37 00:01:19,980 --> 00:01:22,620 and I just clicked it and I'm gonna type Proj1, 38 00:01:22,620 --> 00:01:24,990 that's the name of that folder 39 00:01:24,990 --> 00:01:27,660 which is also going to be the name of my first project. 40 00:01:27,660 --> 00:01:29,670 And then within that, I want to select 41 00:01:29,670 --> 00:01:32,460 this icon right here, new file. 42 00:01:32,460 --> 00:01:35,850 And that's going to be main.cpp. 43 00:01:35,850 --> 00:01:38,760 That is going to be my C++ program. 44 00:01:38,760 --> 00:01:40,650 That's what we're going to build and run. 45 00:01:40,650 --> 00:01:42,180 So this font is a little bit small, 46 00:01:42,180 --> 00:01:43,078 'cause I just installed this 47 00:01:43,078 --> 00:01:45,420 so let me just increase it a little bit. 48 00:01:45,420 --> 00:01:48,330 And the way I did that was just COMMAND +. 49 00:01:48,330 --> 00:01:49,320 All right, so let's write 50 00:01:49,320 --> 00:01:52,110 a really, really simple C++ program here 51 00:01:52,110 --> 00:01:54,600 and then we will configure those extensions 52 00:01:54,600 --> 00:01:56,070 so that they can build and run it. 53 00:01:56,070 --> 00:01:58,020 So let's start with something, 54 00:01:58,020 --> 00:02:00,153 pound include 00:02:03,990 That allows us to do input and output. 56 00:02:03,990 --> 00:02:07,410 We're gonna say using namespace std 57 00:02:07,410 --> 00:02:09,930 and now we're gonna create the main function. 58 00:02:09,930 --> 00:02:11,880 If this looks foreign to you 59 00:02:11,880 --> 00:02:13,050 and you've just started the course, 60 00:02:13,050 --> 00:02:15,210 don't worry, that's perfectly normal. 61 00:02:15,210 --> 00:02:16,294 Just follow along, type along. 62 00:02:16,294 --> 00:02:17,820 The whole point of these lectures 63 00:02:17,820 --> 00:02:20,610 is to make sure we've got the environment set up correctly. 64 00:02:20,610 --> 00:02:22,350 All the syntax and everything you're seeing 65 00:02:22,350 --> 00:02:24,450 is going to be explained in the course. 66 00:02:24,450 --> 00:02:25,540 So at this point, what we're gonna say is, 67 00:02:25,540 --> 00:02:30,540 we're gonna say, cout << "Hello from Proj1" <<, 68 00:02:31,740 --> 00:02:35,130 and we'll put an end line at the end to give us a new line. 69 00:02:35,130 --> 00:02:37,260 And we return zero from main 70 00:02:37,260 --> 00:02:38,910 to indicate that everything's good. 71 00:02:38,910 --> 00:02:41,550 And I just saved that with COMMAND + S. 72 00:02:41,550 --> 00:02:42,511 First thing we're going to do, 73 00:02:42,511 --> 00:02:44,100 is we're gonna set up IntelliSense. 74 00:02:44,100 --> 00:02:46,110 IntelliSense is a little program 75 00:02:46,110 --> 00:02:48,810 that runs alongside Visual Studio Code 76 00:02:48,810 --> 00:02:49,830 that watches what we're typing 77 00:02:49,830 --> 00:02:51,955 and it tries to give us suggestions 78 00:02:51,955 --> 00:02:53,580 and it shows us when we have errors 79 00:02:53,580 --> 00:02:54,810 and it's very, very handy. 80 00:02:54,810 --> 00:02:56,100 So let's set that up. 81 00:02:56,100 --> 00:02:59,010 And the way we do that, we come up to view 82 00:02:59,010 --> 00:03:00,540 and then COMMAND + PALLET. 83 00:03:00,540 --> 00:03:03,720 We can also use SHIFT + COMMAND + P. 84 00:03:03,720 --> 00:03:06,090 And that's going to give us a bunch of options here. 85 00:03:06,090 --> 00:03:08,910 And if you just type C++ at the top, 86 00:03:08,910 --> 00:03:11,190 what we're looking for is this guy right here, 87 00:03:11,190 --> 00:03:14,730 C/C++ Edit Configurations (UI). 88 00:03:14,730 --> 00:03:16,950 We need to make a couple of changes to that. 89 00:03:16,950 --> 00:03:18,270 So I'm gonna open that up 90 00:03:18,270 --> 00:03:20,010 and I'm gonna come right down here 91 00:03:20,010 --> 00:03:22,110 to where it says compiler path. 92 00:03:22,110 --> 00:03:24,180 And if I bring that back a little bit 93 00:03:24,180 --> 00:03:26,070 so I can see this little arrow down here, 94 00:03:26,070 --> 00:03:29,250 I wanna change that to G++. 95 00:03:29,250 --> 00:03:32,130 So I want you to choose G++ right there. 96 00:03:32,130 --> 00:03:34,680 That's the compiler that's going to be used 97 00:03:34,680 --> 00:03:36,780 and then I'm gonna scroll down just a little bit more 98 00:03:36,780 --> 00:03:39,300 until I get to the C++ standard 99 00:03:39,300 --> 00:03:43,440 and there I want to select C++ 17. 100 00:03:43,440 --> 00:03:44,700 That's it, that's all we need to do. 101 00:03:44,700 --> 00:03:46,050 And now if you save this 102 00:03:46,050 --> 00:03:48,153 and I'm saving right now with COMMAND + S. 103 00:03:49,560 --> 00:03:52,440 Now notice what's happened here over on the left-hand side, 104 00:03:52,440 --> 00:03:56,250 a folder was automatically created called .vscode 105 00:03:56,250 --> 00:03:57,766 and inside that folder there's a file called 106 00:03:57,766 --> 00:04:00,810 c_cpp_properties.json. 107 00:04:00,810 --> 00:04:03,360 That file contains the configuration information 108 00:04:03,360 --> 00:04:05,760 for what we just did. So now what we need to do, 109 00:04:05,760 --> 00:04:08,760 is we need to set up a build task 110 00:04:08,760 --> 00:04:11,310 that will execute and build our program. 111 00:04:11,310 --> 00:04:13,260 And the first thing we have to do in order to do that, 112 00:04:13,260 --> 00:04:16,529 we have to select that main.cpp file. 113 00:04:16,529 --> 00:04:17,850 This is very important. 114 00:04:17,850 --> 00:04:19,740 Every time that we need to build something 115 00:04:19,740 --> 00:04:21,690 in Visual Studio Code, 116 00:04:21,690 --> 00:04:25,740 we need to select that main file that we're trying to build. 117 00:04:25,740 --> 00:04:27,870 So we need to define that build task. 118 00:04:27,870 --> 00:04:29,380 So we can come up to terminal 119 00:04:30,270 --> 00:04:33,480 and select configure default build task 120 00:04:33,480 --> 00:04:36,240 and I'll select that and then it's gonna ask us 121 00:04:36,240 --> 00:04:39,270 what build system are you using, what compiler are we using? 122 00:04:39,270 --> 00:04:40,891 Well, we're using G++. 123 00:04:40,891 --> 00:04:42,330 You can see it right here. 124 00:04:42,330 --> 00:04:43,653 I'm gonna select that. 125 00:04:44,640 --> 00:04:46,050 Now it's created a new file, 126 00:04:46,050 --> 00:04:49,270 you can see it over here on the left. It's called tasks.json 127 00:04:50,130 --> 00:04:52,380 and it contains configuration information 128 00:04:52,380 --> 00:04:56,370 for how Visual Studio Code is gonna build our project. 129 00:04:56,370 --> 00:04:58,740 Now we need to make a few changes to this file. 130 00:04:58,740 --> 00:05:00,660 Right here on line nine, you'll see this, 131 00:05:00,660 --> 00:05:03,540 on, sorry, on line eight, you'll see this args section. 132 00:05:03,540 --> 00:05:05,820 These are the arguments that are sent to the compiler 133 00:05:05,820 --> 00:05:07,080 and we're gonna add a couple. 134 00:05:07,080 --> 00:05:10,500 So right after line nine, right after the -g option, 135 00:05:10,500 --> 00:05:15,123 we're gonna put in -Wall with a capital W, 136 00:05:15,990 --> 00:05:17,550 lowercase A-L-L. 137 00:05:17,550 --> 00:05:20,850 That tells the compiler to generate all warnings. 138 00:05:20,850 --> 00:05:25,850 Then we're going to add one more, -std=c++17, 139 00:05:26,520 --> 00:05:30,360 and that's -std=c++17. 140 00:05:30,360 --> 00:05:34,080 That tells the compiler to use the C++ 17 standard. 141 00:05:34,080 --> 00:05:35,280 And there's one more thing we need to do, 142 00:05:35,280 --> 00:05:37,710 is we're going to change this line 12 right here. 143 00:05:37,710 --> 00:05:42,060 We're gonna change that file to this name, fileDirname. 144 00:05:42,060 --> 00:05:43,705 So what I'm gonna do is, I'm just gonna copy that 145 00:05:43,705 --> 00:05:45,760 and I'm gonna paste it right in here 146 00:05:46,800 --> 00:05:48,990 and then right after that curly, 147 00:05:48,990 --> 00:05:53,550 that closed curly brace, I'm just gonna do /*.cpp. 148 00:05:53,550 --> 00:05:55,380 That tells the compiler to compile 149 00:05:55,380 --> 00:05:58,140 all the C++ files into folder. 150 00:05:58,140 --> 00:06:01,620 By default, Visual Studio Code will only compile one file. 151 00:06:01,620 --> 00:06:02,700 We only have one there now, 152 00:06:02,700 --> 00:06:04,830 but we'll have more than one later in the course, 153 00:06:04,830 --> 00:06:07,620 so we wanna be sure that we're compiling all of them. 154 00:06:07,620 --> 00:06:09,750 That's it, those are the changes we need to make. 155 00:06:09,750 --> 00:06:11,250 At this point we can save, 156 00:06:11,250 --> 00:06:15,030 I just saved with COMMAND + S and we can close this down. 157 00:06:15,030 --> 00:06:17,010 Now we're ready to build our program. 158 00:06:17,010 --> 00:06:18,540 So remember what I told you, 159 00:06:18,540 --> 00:06:19,920 the first thing you need to do, 160 00:06:19,920 --> 00:06:23,790 is select the main.cpp file that you want to build. 161 00:06:23,790 --> 00:06:27,750 There may be and there will be many, many projects in here. 162 00:06:27,750 --> 00:06:29,580 We'll have Proj1, Proj2. 163 00:06:29,580 --> 00:06:32,040 Each one of them will have its own main.cpp, 164 00:06:32,040 --> 00:06:33,510 so we have to be sure that we select 165 00:06:33,510 --> 00:06:34,920 the one that we want to build 166 00:06:34,920 --> 00:06:38,700 and then we're gonna come up to terminal and run build task. 167 00:06:38,700 --> 00:06:41,550 You can also do SHIFT + COMMAND + B. 168 00:06:41,550 --> 00:06:44,697 When we do that, the compiler will build the executable file 169 00:06:44,697 --> 00:06:46,170 that we want to run. 170 00:06:46,170 --> 00:06:47,520 So I'm gonna click that now 171 00:06:48,420 --> 00:06:50,220 and you can see the output right here. 172 00:06:50,220 --> 00:06:52,560 Notice we're using the G++ compiler. 173 00:06:52,560 --> 00:06:57,560 There's that -g -Wall -std=c++17 we entered. 174 00:06:57,780 --> 00:07:00,420 There's the *cpp we entered 175 00:07:00,420 --> 00:07:04,440 and it's building main in Proj1 folder. 176 00:07:04,440 --> 00:07:05,970 That's exactly what we want. 177 00:07:05,970 --> 00:07:07,710 Build finished successfully. 178 00:07:07,710 --> 00:07:11,820 So if you notice, a file was created over here, right there. 179 00:07:11,820 --> 00:07:13,380 That main file. 180 00:07:13,380 --> 00:07:16,260 That is our executable file, that's the file we can run. 181 00:07:16,260 --> 00:07:18,570 That's the file that's gonna say "Hello from Proj1" 182 00:07:18,570 --> 00:07:19,650 when we execute it. 183 00:07:19,650 --> 00:07:20,610 How do you execute it? 184 00:07:20,610 --> 00:07:22,380 Real simple, I'm just gonna close this here 185 00:07:22,380 --> 00:07:24,330 to gimme a little bit more room 186 00:07:24,330 --> 00:07:26,850 and I am gonna right click on that 187 00:07:26,850 --> 00:07:29,523 and then say open in integrated terminal. 188 00:07:30,720 --> 00:07:33,000 And it's just opened that folder for me. 189 00:07:33,000 --> 00:07:35,015 And if I type ls, it'll give me a listing 190 00:07:35,015 --> 00:07:37,560 of the files in that folder and you can see, 191 00:07:37,560 --> 00:07:40,050 you can see main, you can see main.cpp 192 00:07:40,050 --> 00:07:42,330 and you also see this main symbol file. 193 00:07:42,330 --> 00:07:44,550 That's something that the compiler generates. 194 00:07:44,550 --> 00:07:47,640 So if we want to run this program, we need to run main. 195 00:07:47,640 --> 00:07:50,640 We do ./main. 196 00:07:50,640 --> 00:07:52,890 That dot slash is real important. 197 00:07:52,890 --> 00:07:54,690 So always use dot forward slash 198 00:07:54,690 --> 00:07:56,310 and then the name of the executable. 199 00:07:56,310 --> 00:07:57,270 And when I press ENTER, 200 00:07:57,270 --> 00:07:59,700 we should see "Hello from Proj1". 201 00:07:59,700 --> 00:08:02,610 And there it is, that's exactly what we expected. 202 00:08:02,610 --> 00:08:04,050 Pretty cool, pretty easy. 203 00:08:04,050 --> 00:08:06,460 Now, suppose I want to create another project 204 00:08:07,350 --> 00:08:08,370 right under here. 205 00:08:08,370 --> 00:08:11,030 If I click on that folder icon right there 206 00:08:11,030 --> 00:08:13,530 to create a new folder, it won't do what I want. 207 00:08:13,530 --> 00:08:15,450 What it'll do is, it'll create that new folder 208 00:08:15,450 --> 00:08:16,950 underneath Proj1. 209 00:08:16,950 --> 00:08:18,330 That's not what I want. 210 00:08:18,330 --> 00:08:22,020 I want Proj2 to be at the same level as Proj1. 211 00:08:22,020 --> 00:08:24,390 Intuition says, come on up here and click it, right? 212 00:08:24,390 --> 00:08:26,160 But that doesn't really work. 213 00:08:26,160 --> 00:08:27,420 So it's not too intuitive, 214 00:08:27,420 --> 00:08:30,210 but the way that you do it is you can select Proj1 215 00:08:30,210 --> 00:08:31,950 and then just hit ESCAPE. 216 00:08:31,950 --> 00:08:33,630 So you see, I just pressed the ESCAPE key 217 00:08:33,630 --> 00:08:35,460 and I got this blue box. 218 00:08:35,460 --> 00:08:37,799 Now I can click on our folder 219 00:08:37,799 --> 00:08:40,620 and create a new project called Proj2 220 00:08:40,620 --> 00:08:43,320 and it's going to be at the same level as Proj1. 221 00:08:43,320 --> 00:08:46,090 Now I can create my main.cpp file in here 222 00:08:47,400 --> 00:08:48,690 and I can write another program 223 00:08:48,690 --> 00:08:49,920 and I'm actually not gonna write it, 224 00:08:49,920 --> 00:08:52,710 I'm just gonna copy this program since it's almost the same. 225 00:08:52,710 --> 00:08:55,450 So I'm just gonna copy this program from here 226 00:08:56,670 --> 00:08:59,640 right into here, and we'll just make one change. 227 00:08:59,640 --> 00:09:00,683 We'll say Proj2. 228 00:09:01,923 --> 00:09:03,240 That's pretty easy. 229 00:09:03,240 --> 00:09:06,750 Now if we run this, remember we have to build it first. 230 00:09:06,750 --> 00:09:08,880 We have to select which one we want to build. 231 00:09:08,880 --> 00:09:10,680 Well, we wanna build Proj2. 232 00:09:10,680 --> 00:09:13,050 So make sure you select the main in Proj2. 233 00:09:13,050 --> 00:09:15,270 We don't wanna select the main in Proj1. 234 00:09:15,270 --> 00:09:16,770 We've already done that one. 235 00:09:16,770 --> 00:09:18,570 So I'm gonna select that 236 00:09:18,570 --> 00:09:19,740 and then I'm gonna build. 237 00:09:19,740 --> 00:09:23,853 So I'm gonna go to terminal and select run the build task. 238 00:09:25,080 --> 00:09:27,390 And we're done, you can see build finished successfully 239 00:09:27,390 --> 00:09:29,853 and Proj2's main was created. 240 00:09:30,840 --> 00:09:32,100 And there it is right here. 241 00:09:32,100 --> 00:09:33,420 So if I wanna execute that, 242 00:09:33,420 --> 00:09:36,300 I can just open in the integrated terminal. 243 00:09:36,300 --> 00:09:38,400 We can do ls again if we want. 244 00:09:38,400 --> 00:09:41,430 There it is and we do ./main 245 00:09:41,430 --> 00:09:43,740 and it's gonna say "Hello from Proj2". 246 00:09:43,740 --> 00:09:44,850 Pretty easy. 247 00:09:44,850 --> 00:09:46,301 We can modify this program slightly 248 00:09:46,301 --> 00:09:47,910 to get input from the user. 249 00:09:47,910 --> 00:09:48,870 Let's do that. 250 00:09:48,870 --> 00:09:51,330 Let's say we have an integer 251 00:09:51,330 --> 00:09:55,560 and we'll call that integer fav_number, just like that. 252 00:09:55,560 --> 00:09:57,480 And then we'll keep this "Hello from Proj2" in there, 253 00:09:57,480 --> 00:09:59,940 but let's also ask the user to enter their favorite number. 254 00:09:59,940 --> 00:10:03,720 So we're gonna say, "Enter your favorite number". 255 00:10:03,720 --> 00:10:05,760 Then we're gonna read that number 256 00:10:05,760 --> 00:10:07,920 into the favorite number variable 257 00:10:07,920 --> 00:10:10,290 and now we're going to display 258 00:10:10,290 --> 00:10:13,110 and now we're going to display your favorite number 259 00:10:13,110 --> 00:10:14,710 is the number they just entered. 260 00:10:18,030 --> 00:10:20,460 And we'll add a new line at the end. 261 00:10:20,460 --> 00:10:23,490 That's it, so we've just modified that program slightly. 262 00:10:23,490 --> 00:10:25,860 If we execute main now, 263 00:10:25,860 --> 00:10:27,390 the old program is going to run, 264 00:10:27,390 --> 00:10:30,030 because we haven't built the new one yet, right? 265 00:10:30,030 --> 00:10:33,030 These updates are not in this main executable yet. 266 00:10:33,030 --> 00:10:34,380 That's why we need to build it. 267 00:10:34,380 --> 00:10:37,620 So we're gonna select our main.cpp 268 00:10:37,620 --> 00:10:40,200 and I'm going to select terminal. 269 00:10:40,200 --> 00:10:41,640 Run the build task. 270 00:10:41,640 --> 00:10:43,380 We have a successful build. 271 00:10:43,380 --> 00:10:46,290 Now I can come over here to my main right, click on it, 272 00:10:46,290 --> 00:10:48,963 open it in the terminal and we could just run it. 273 00:10:50,760 --> 00:10:51,990 Hello from Proj2. 274 00:10:51,990 --> 00:10:53,568 Now it's asking me to enter my favorite number. 275 00:10:53,568 --> 00:10:56,130 Well, my favorite number let's say is 34. 276 00:10:56,130 --> 00:10:57,300 I'll hit ENTER. 277 00:10:57,300 --> 00:11:00,120 It says your favorite number is 34. 278 00:11:00,120 --> 00:11:01,350 Simple as that. 279 00:11:01,350 --> 00:11:03,840 We can create as many projects as we like. 280 00:11:03,840 --> 00:11:05,376 Once our tasks.json 281 00:11:05,376 --> 00:11:09,240 and our cpp_properties.json files are configured, 282 00:11:09,240 --> 00:11:10,890 then we can create as many as we want. 283 00:11:10,890 --> 00:11:12,660 We don't have to touch those again. 284 00:11:12,660 --> 00:11:15,270 They'll apply to all the sub-projects. 285 00:11:15,270 --> 00:11:18,720 So in the next video, I'll show you how to use the debugger 286 00:11:18,720 --> 00:11:20,610 and how to configure the debugger 287 00:11:20,610 --> 00:11:23,550 so that we can walk through our code one line at a time 288 00:11:23,550 --> 00:11:25,464 and inspect our variables and their values 289 00:11:25,464 --> 00:11:27,390 as we execute the code. 290 00:11:27,390 --> 00:11:29,490 It's a very, very handy tool 291 00:11:29,490 --> 00:11:31,530 that we can use to help us program. 292 00:11:31,530 --> 00:11:33,080 I'll see you in the next video.