In this video, we'll go over the steps necessary to write,
build and execute c++ programs
from the command line on a mac.
All we really need is a terminal session and an editor.
Now as far as the editor is concerned, there are a bunch of editors out there. Some of
them are free, some of them are paid editors.
The one I'm going to use in this example is called Atom.
And you can download it from the internet.
And it's absolutely free. And it's a fine text editor.
Now before I do that, I'm going to create a folder on my desktop.
And I'm going to call it cpp projects.
And within that folder, I'm going to create another folder called project1.
And this is where we're going to store our source files. Okay.
So now what we need is we need an editor and we need a terminal window.
So let's create both of them first. Let's, create the terminal session.
So we'll go to spotlight search and type terminal and press enter.
And what I'm going to do is I'm going to move this terminal over here on the right.
So that it's right docked just perfectly.
And I'm going to run the Atom editor now. Again, you can run any editor you like.
And there's the Atom editor, and I'll close down some of these tabs here
so that we're here from scratch perfect.
And again, I'm going to just dock this over here on the left side.
Okay. So now what we want to do is we want to create a file
in that folder that we just created. So let's go to file,
new file,
and it's going to be c++ file. So I'm going to save this.
Right now so that Atom knows that it's a c++ file. I'm going to say file,
save as
and I want to put this on my desktop,
in my cpp projects, project1 folder.
And I want to name the file main.cpp.
That's it. I'll save it and now we can start typing our c++ program.
It's really straightforward that simple hello world program is what we're going to write.
So we're going to say pound include,
and you can see now that we've saved it as a .cpp file,
Atom knows that it's c++
so it's going to allow us to -- it's going to help us out with the
code highlighting and syntax highlighting.
So I'm going to include iostream,
then I'm going to have my main. I'll say int main
open close parents
and open curly, which it completes for me.
And now here all I'm going to say is std::
cout then followed by the insertion operator,
which is the two << signs together. And I'll just say
any string.
I'll just say hello mac command line.
And I'll terminate that string.
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.
So that's endl. And we'll return 0.
That's it. That's the program. We'll save it.
Now we need to go over to our terminal to compile and run it.
When you open up the terminal, it defaults in your home directory.
So you can see if you type pwd that prints the working directory,
which is users followed by your username
that's not where I want to be. I want to be in the desktop folder that we created,
so we can use the cd command to change directory.
And I'll just type desktop and you can press tab to auto complete.
So now I'm in the desktop directory,
then I want to go into my cpp projects directory and finally into my project1 directory,
that's where I'm at. Now if I want to know what's in there, you can just type
LS, which gives you a file listing.
And you can see there's main.cpp.That's this file we just created in the editor.
What we want to do now is we want to compile this file.
So what we want to do is we want to type g++
that's the name of the compiler that'll execute the compiler.
Then we want to provide the compiler the same switches that we did in the IDE.
So we'll say -Wall.
So that turns on all warnings. Then we want to write -
std=c++14,
that'll tell us tell it to use modern c++ the 14 standard.
Now we tell it what files you want to compile.
In this case, I only have one, main.cpp.
If you have more than one, you would just list them all separated by spaces.
But here, we've got main.cpp.
That's it. That's all you need to do. You press enter,
we get no error messages. No news is good news.
We'll do an LS again, and you can see that now we've got a new file created
called a .out.
That's the default executable file's name when you don't provide a name.
In order to run this, you just type ./a.out.
When I press enter, we should see hello mac command line.
That's exactly what we see. That's this statement right here executing.
It's as simple as that.
Now if you don't like the a.out, you want to provide your own file name.
You can come up and what I'm doing here is I'm pressing the up arrow
and the down arrow. You can see it's going through what I've typed.
So you want to go back to the statement where you ran the compiler
and just type a -o and whatever name you want
your executable to be. And you press enter.
Now if we do a directory listing, you can see that main is there. If you want to run main
./main.
And that's it. We've created a file,
a c++ program.
We compiled it, and we ran it using only the command line,
no IDEs.
And as I mentioned, there's a whole bunch of editors out there. You can use Atom, brackets,
text wrangler, text mate,
sublime. And there's a bunch much more you can just do a search
on google, and you can come up with a bunch of examples of text editors.
Okay. So let's talk about compiler errors now.
I'm going to clear the screen here, and I can do that in the terminal by just typing clear.
And let's get rid of that semicolon at the end here.
Now if I run the compiler again,
you can see I got a clean compile.
That makes no sense because I created an error here by removing the semicolon.
Well, the issue is that I never saved this file.
When you work with the IDEs, the IDEs sync everything up.
So they realize that your source code has changed.
So they're going to compile the latest version of it.
Here these two are two separate programs and they're not synced.
You're the person that has to sync them together by making sure you save it.
So in this case, I'm going to come over here and do file save
or command s.
Now when I come back here and compile again, I'll see the error,
which says I'm missing a semicolon. So I'll add that semicolon.
If I put a semicolon in here at the end of iostream,
which I should not have, I'll save it.
And then I'll go compile it again.
You can see there's an extra token at the end of the line. It's complaining about it.
So I can go back here,
command s again to save, and I can recompile. This time I'll get a clean compile.
And I can run my main.
That's it it's as simple as that.
If you don't have the resources to run an IDE or you don't like the IDE or you feel
the IDE is confusing you and not really allowing you to focus on the c++,
this is a great alternative, pretty easy, pretty fast.
And it will be in business real quick.