1 00:00:05,600 --> 00:00:09,200 In this video, we'll go over the steps necessary to write, 2 00:00:09,700 --> 00:00:12,700 build and execute c++ programs 3 00:00:12,700 --> 00:00:14,500 from the command line on a mac. 4 00:00:15,300 --> 00:00:19,300 All we really need is a terminal session and an editor. 5 00:00:19,500 --> 00:00:23,500 Now as far as the editor is concerned, there are a bunch of editors out there. Some of 6 00:00:23,500 --> 00:00:26,500 them are free, some of them are paid editors. 7 00:00:26,500 --> 00:00:29,000 The one I'm going to use in this example is called Atom. 8 00:00:29,000 --> 00:00:32,000 And you can download it from the internet. 9 00:00:32,000 --> 00:00:35,300 And it's absolutely free. And it's a fine text editor. 10 00:00:35,660 --> 00:00:39,160 Now before I do that, I'm going to create a folder on my desktop. 11 00:00:39,160 --> 00:00:41,360 And I'm going to call it cpp projects. 12 00:00:43,060 --> 00:00:47,660 And within that folder, I'm going to create another folder called project1. 13 00:00:51,320 --> 00:00:55,320 And this is where we're going to store our source files. Okay. 14 00:00:56,310 --> 00:00:59,410 So now what we need is we need an editor and we need a terminal window. 15 00:00:59,410 --> 00:01:03,210 So let's create both of them first. Let's, create the terminal session. 16 00:01:03,210 --> 00:01:06,810 So we'll go to spotlight search and type terminal and press enter. 17 00:01:07,060 --> 00:01:10,060 And what I'm going to do is I'm going to move this terminal over here on the right. 18 00:01:11,060 --> 00:01:13,660 So that it's right docked just perfectly. 19 00:01:14,210 --> 00:01:18,570 And I'm going to run the Atom editor now. Again, you can run any editor you like. 20 00:01:20,770 --> 00:01:24,130 And there's the Atom editor, and I'll close down some of these tabs here 21 00:01:25,010 --> 00:01:27,210 so that we're here from scratch perfect. 22 00:01:27,760 --> 00:01:30,160 And again, I'm going to just dock this over here on the left side. 23 00:01:31,960 --> 00:01:34,460 Okay. So now what we want to do is we want to create a file 24 00:01:34,460 --> 00:01:37,260 in that folder that we just created. So let's go to file, 25 00:01:37,760 --> 00:01:38,640 new file, 26 00:01:39,890 --> 00:01:42,490 and it's going to be c++ file. So I'm going to save this. 27 00:01:42,490 --> 00:01:46,750 Right now so that Atom knows that it's a c++ file. I'm going to say file, 28 00:01:47,350 --> 00:01:48,340 save as 29 00:01:51,740 --> 00:01:53,640 and I want to put this on my desktop, 30 00:01:58,890 --> 00:02:01,770 in my cpp projects, project1 folder. 31 00:02:02,020 --> 00:02:04,220 And I want to name the file main.cpp. 32 00:02:08,580 --> 00:02:12,780 That's it. I'll save it and now we can start typing our c++ program. 33 00:02:13,680 --> 00:02:17,180 It's really straightforward that simple hello world program is what we're going to write. 34 00:02:17,180 --> 00:02:19,180 So we're going to say pound include, 35 00:02:20,480 --> 00:02:23,810 and you can see now that we've saved it as a .cpp file, 36 00:02:23,810 --> 00:02:25,810 Atom knows that it's c++ 37 00:02:25,810 --> 00:02:28,810 so it's going to allow us to -- it's going to help us out with the 38 00:02:29,170 --> 00:02:31,380 code highlighting and syntax highlighting. 39 00:02:31,380 --> 00:02:33,380 So I'm going to include iostream, 40 00:02:35,880 --> 00:02:38,780 then I'm going to have my main. I'll say int main 41 00:02:39,280 --> 00:02:40,570 open close parents 42 00:02:41,570 --> 00:02:44,570 and open curly, which it completes for me. 43 00:02:44,930 --> 00:02:48,630 And now here all I'm going to say is std:: 44 00:02:48,630 --> 00:02:51,930 cout then followed by the insertion operator, 45 00:02:51,930 --> 00:02:55,830 which is the two << signs together. And I'll just say 46 00:02:56,330 --> 00:02:57,030 any string. 47 00:02:57,030 --> 00:03:00,390 I'll just say hello mac command line. 48 00:03:02,590 --> 00:03:04,360 And I'll terminate that string. 49 00:03:04,360 --> 00:03:08,720 And then I'll put a new line at the end. So that the cursor goes to the next line when we do the output. 50 00:03:09,720 --> 00:03:13,320 So that's endl. And we'll return 0. 51 00:03:14,620 --> 00:03:17,120 That's it. That's the program. We'll save it. 52 00:03:19,520 --> 00:03:22,520 Now we need to go over to our terminal to compile and run it. 53 00:03:23,020 --> 00:03:26,120 When you open up the terminal, it defaults in your home directory. 54 00:03:26,120 --> 00:03:29,920 So you can see if you type pwd that prints the working directory, 55 00:03:29,920 --> 00:03:32,580 which is users followed by your username 56 00:03:32,580 --> 00:03:36,480 that's not where I want to be. I want to be in the desktop folder that we created, 57 00:03:36,480 --> 00:03:39,480 so we can use the cd command to change directory. 58 00:03:39,880 --> 00:03:43,480 And I'll just type desktop and you can press tab to auto complete. 59 00:03:43,980 --> 00:03:45,980 So now I'm in the desktop directory, 60 00:03:45,980 --> 00:03:50,680 then I want to go into my cpp projects directory and finally into my project1 directory, 61 00:03:50,680 --> 00:03:53,680 that's where I'm at. Now if I want to know what's in there, you can just type 62 00:03:53,680 --> 00:03:55,680 LS, which gives you a file listing. 63 00:03:55,980 --> 00:04:00,340 And you can see there's main.cpp.That's this file we just created in the editor. 64 00:04:01,140 --> 00:04:03,640 What we want to do now is we want to compile this file. 65 00:04:04,140 --> 00:04:06,540 So what we want to do is we want to type g++ 66 00:04:06,540 --> 00:04:09,540 that's the name of the compiler that'll execute the compiler. 67 00:04:10,040 --> 00:04:13,700 Then we want to provide the compiler the same switches that we did in the IDE. 68 00:04:13,700 --> 00:04:17,600 So we'll say -Wall. 69 00:04:18,200 --> 00:04:21,700 So that turns on all warnings. Then we want to write - 70 00:04:21,700 --> 00:04:25,900 std=c++14, 71 00:04:26,560 --> 00:04:30,220 that'll tell us tell it to use modern c++ the 14 standard. 72 00:04:31,520 --> 00:04:34,020 Now we tell it what files you want to compile. 73 00:04:34,020 --> 00:04:37,320 In this case, I only have one, main.cpp. 74 00:04:37,320 --> 00:04:41,320 If you have more than one, you would just list them all separated by spaces. 75 00:04:41,520 --> 00:04:43,720 But here, we've got main.cpp. 76 00:04:44,710 --> 00:04:48,010 That's it. That's all you need to do. You press enter, 77 00:04:48,560 --> 00:04:51,860 we get no error messages. No news is good news. 78 00:04:53,160 --> 00:04:56,660 We'll do an LS again, and you can see that now we've got a new file created 79 00:04:56,660 --> 00:04:57,960 called a .out. 80 00:04:58,460 --> 00:05:02,560 That's the default executable file's name when you don't provide a name. 81 00:05:03,060 --> 00:05:07,060 In order to run this, you just type ./a.out. 82 00:05:07,060 --> 00:05:10,060 When I press enter, we should see hello mac command line. 83 00:05:10,060 --> 00:05:13,660 That's exactly what we see. That's this statement right here executing. 84 00:05:14,460 --> 00:05:15,760 It's as simple as that. 85 00:05:16,260 --> 00:05:19,760 Now if you don't like the a.out, you want to provide your own file name. 86 00:05:20,120 --> 00:05:23,720 You can come up and what I'm doing here is I'm pressing the up arrow 87 00:05:23,720 --> 00:05:26,720 and the down arrow. You can see it's going through what I've typed. 88 00:05:27,380 --> 00:05:30,380 So you want to go back to the statement where you ran the compiler 89 00:05:30,380 --> 00:05:34,980 and just type a -o and whatever name you want 90 00:05:35,180 --> 00:05:37,780 your executable to be. And you press enter. 91 00:05:38,580 --> 00:05:42,680 Now if we do a directory listing, you can see that main is there. If you want to run main 92 00:05:42,680 --> 00:05:43,870 ./main. 93 00:05:45,370 --> 00:05:48,670 And that's it. We've created a file, 94 00:05:48,670 --> 00:05:50,170 a c++ program. 95 00:05:50,720 --> 00:05:54,080 We compiled it, and we ran it using only the command line, 96 00:05:54,080 --> 00:05:55,480 no IDEs. 97 00:05:55,880 --> 00:06:00,080 And as I mentioned, there's a whole bunch of editors out there. You can use Atom, brackets, 98 00:06:00,080 --> 00:06:03,380 text wrangler, text mate, 99 00:06:04,780 --> 00:06:09,380 sublime. And there's a bunch much more you can just do a search 100 00:06:09,380 --> 00:06:12,880 on google, and you can come up with a bunch of examples of text editors. 101 00:06:13,680 --> 00:06:17,380 Okay. So let's talk about compiler errors now. 102 00:06:17,380 --> 00:06:21,780 I'm going to clear the screen here, and I can do that in the terminal by just typing clear. 103 00:06:23,280 --> 00:06:25,780 And let's get rid of that semicolon at the end here. 104 00:06:27,080 --> 00:06:29,080 Now if I run the compiler again, 105 00:06:30,440 --> 00:06:32,140 you can see I got a clean compile. 106 00:06:32,140 --> 00:06:36,540 That makes no sense because I created an error here by removing the semicolon. 107 00:06:36,900 --> 00:06:40,200 Well, the issue is that I never saved this file. 108 00:06:40,200 --> 00:06:44,000 When you work with the IDEs, the IDEs sync everything up. 109 00:06:44,000 --> 00:06:46,600 So they realize that your source code has changed. 110 00:06:46,850 --> 00:06:49,450 So they're going to compile the latest version of it. 111 00:06:49,450 --> 00:06:52,450 Here these two are two separate programs and they're not synced. 112 00:06:52,450 --> 00:06:55,750 You're the person that has to sync them together by making sure you save it. 113 00:06:55,750 --> 00:06:58,750 So in this case, I'm going to come over here and do file save 114 00:06:59,950 --> 00:07:01,050 or command s. 115 00:07:02,050 --> 00:07:05,450 Now when I come back here and compile again, I'll see the error, 116 00:07:06,150 --> 00:07:09,950 which says I'm missing a semicolon. So I'll add that semicolon. 117 00:07:11,350 --> 00:07:14,550 If I put a semicolon in here at the end of iostream, 118 00:07:14,550 --> 00:07:16,950 which I should not have, I'll save it. 119 00:07:17,450 --> 00:07:19,450 And then I'll go compile it again. 120 00:07:19,450 --> 00:07:23,440 You can see there's an extra token at the end of the line. It's complaining about it. 121 00:07:23,440 --> 00:07:24,440 So I can go back here, 122 00:07:26,440 --> 00:07:30,840 command s again to save, and I can recompile. This time I'll get a clean compile. 123 00:07:30,840 --> 00:07:32,240 And I can run my main. 124 00:07:33,540 --> 00:07:35,540 That's it it's as simple as that. 125 00:07:35,540 --> 00:07:39,540 If you don't have the resources to run an IDE or you don't like the IDE or you feel 126 00:07:39,540 --> 00:07:43,740 the IDE is confusing you and not really allowing you to focus on the c++, 127 00:07:43,740 --> 00:07:47,410 this is a great alternative, pretty easy, pretty fast. 128 00:07:47,410 --> 00:07:50,010 And it will be in business real quick.