In this video, we'll go over the steps necessary to create,
build and run
a c++ program on ubuntu Linux
using only a text editor in the command line.
So in this video, we won't be using any integrated development environment.
First of all,
I've created a folder on my desktop called cpp projects.
And in that folder, I've created another folder called project1.
This is where we're going to store our c++ program.
In this case, it's just going to be a main, a real simple test.
So I'd like to close this up
and the first thing we need to do is we need some sort of editor to type in our program.
Now on Linux, we've got gedit.
So I could just come to search my computer and type gedit.
And we can use that text editor which is a fine text editor. It's real simple.
It comes with your installation.
There are many other text editors and programmer text editors that you can use,
like brackets,
atom and many others but this one is already here.
So we may as well use it and it works just fine.
I've gone ahead and gone to edit preferences.
And I just turned on line numbers, and you can set all kinds of different settings that you like.
I made the font also a little bigger so you can see it.
So it may look a little different when you start yours.
So the first thing we want to do is we want to write the c++ program. So let's do it.
Include
io stream just like we've done before.
I'll leave a space and then I'll say int main,
two parentheses, the open curly,
and I'll close the curly while I'm at it.
And now we'll just say we could just press tab.
We'll say std::cout.
The insertion operator which is the two << signs,
break together with no spaces. And I'll say "Hello
Ubuntu command line".
We'll close the string.
We'll put another insertion operator,
and let's put a new line at the end that way our cursor will go to the next line when we do the output.
And that's e-n-d-l followed by a semi-colon.
And then we return 0 just like we always do.
That's it. That's our c++ program.
Now it's important that we save this with a .cpp extension and not
a txt extension.
Z++ compiler does not like txt extensions. And you're going to get all kinds of
weird errors.
Okay. So let's do that. Let's go to file,
save as.
We'll find where we want to save it. We want to save it to our desktop,
cpp projects, project1.
That's where we would like to save it. And I'll give it the name as main.cpp.
Now you'll notice a couple of things. When I name the file cpp,
the editor is smart.
So it realizes that this is not a regular text file. It's a c++ text file,
so I'll save it. And notice what happened here.
Notice the color coding, it knows c++.
Again, it's not an integrated development environment.
So it doesn't have a lot of hooks to the compiler and so forth.
But you can see that now it's really helping me
because I can see that my string is terminated.
For example, if I get rid of that guy,
then that's black and it shouldn't be black, it should be a magenta color.
So again, I'm going to press ctrl s to save that file.
Now we'd like to compile and run it.
So in order to do that, we need to open up a terminal.
So I'll come up back up here, and I'll say terminal.
And that'll open up a terminal, which I'll pull down just right
by here so we can run them together.
Now one of the things that you'll notice is that
if you do a pwd, which prints your working directory,
I'm in home Frank.
I don't want to be in home Frank. I want to be in this directory, right up here that you can see desktop,
cpp projects, project1.
So I need to change into that directory. We can use a cd command
to change directories. So I want to go to my desktop,
and you can press tab for auto completion,
and press enter.
Then I want to press -- then I want to type cd
cpp projects.
And then finally, project1.
And you could do this all in one line if you like. I just did it in three steps.
So now I'm in the correct directory.
If I want to know what's in that directory, I can just type LS, which gives me a listing of the directory.
And you can see that my main cpp file is there.
That's the one that I want to compile.
So let me make this thing a little bit wider just you can see what I'm typing here.
So the first thing I want to do is I want to execute the compiler.
So I want to say g++, that calls the compiler.
That's the gnu c++ compiler.
And I want to set some options. These are the same options that we would set with the IDE.
Here, we're just going to set them as switches right on the command line.
So I'm going to say Wall,
that turns on all warnings.
Then I want to say -std=
c++ 14,
that's going to use the for the 2014 c++ standard,
which is the latest one that's out there.
Now we supply what do you want to compile? Well, I want to compile the main.cpp file.
Main.cpp. That's it. So it's going to execute the compiler,
turn on all warnings,
use the latest standard and compile that one file, main.cpp.
I'll press enter.
I got back no messages, which is good as they say no news is good news.
If you have errors, it'll tell you. If you don't, it'll be nice and quiet.
At this point, I'll do an LS again to see what's in our directory.
And you can see that it's created a file called a.out.
That's the common normal file,
executable file that's created when you don't say hey create main or
create some other name.
To run it, you just type ./a.out.
And there you go. It says hello ubuntu command line, exactly what we typed up here in our program.
If you don't want the a.out, and you want your own executable,
then you could type that in and I'll show you how to do that.
What I'm doing now is I'm pressing the up arrow
and the down arrow and you can go through what you've typed in that way you don't have to type it all in again.
So you want to go back to the statement where you compiled and just type a
-o and the name of the file that you want to create.
So in this case, let's just say main. That'll be my executable.
I'll press enter. I'll do LS again.
Now you can see that there's a main file right there.
And I can now run main. And I get the same output.
Okay. Simple as that it really is very straightforward.
Now one of the things that you have to keep in mind.
Since this is not an IDE, you've got two things going on here separately, right?
You've got your editor here and your command line tool here.
They're not really synced together. So you're the one that's syncing them together.
So if I remove the semicolon from my text editor
and I compile again, I expect an error, right?
I didn't get an error. The reason I didn't get the error was because I never saved the file.
You have to be sure to save the file and then compile.
The IDE sort of handle that magically behind the scenes.
They realize that you've made a change. So it's going to compile the latest changes you made.
This is different since they're not in sync.
So you want to be sure that you can come up here
and save file. Save or control s.
Now I've saved it, now I compile, now I see the error.
So you can see the errors it's expecting a semicolon,
which I can replace if I get rid of this quote mark
and ctrl s. I'm saving again.
Now I run. Now I'm going to get a different error,
something about a terminating quote character is missing.
So I can put that back in,
ctrl s and save and compile again.
And we can run main one more time.
That's it. So that's it. That's pretty straightforward.
These are the steps necessary when you have a plain text editor and the command line.
If you cannot install an IDE for some reason,
maybe you've got minimal hardware requirements, you just don't like IDEs or
you find them too confusing,
don't let that stop you from learning c++.
You can still use a simple text editor like this
and a command line tool and you can see the command, you can see the command right there.
It's really, really straightforward.
If you've got multiple c++ files, which we will later on in the course,
you just type them all in here file1.cpp, file2.cpp and so forth.
It'll compile everything, link everything and run it for you.
Many times you're if you're a consultant and you're working out there, you'll show up at a place,
and there's no IDE. You know you're working as a consultant,
all you need is an editor and a command line, and you're in business.
Okay. So that wraps up this video.