Hello and welcome back.
In this video, I'm going to walk you through how you can compile and run your C programs using a simple
text editor through the command line interface on ubuntu.
So here we have our ubuntu operating system.
Remember to start programs and to start different terminals.
You can actually select the dot dot dot.
And you can come up here and type in search.
And you can say terminal.
That's one way to start it.
If you don't go to the search option, you can actually just come up here and right click on the window
and you can say open in terminal.
And that will bring up a terminal as well.
Now this is your command line interface.
This is the terminal.
This is where you type in commands to run your C programs essentially.
And you'll notice when it first comes up it's going to come up with specific font and your directory
structure wherever you're logged in.
So this comes up on the desktop right.
And so you can issue simple commands like CD to change directories.
This is not going to be an exhaustive tutorial on all the commands and Linux to create directories and
navigate through directories.
I mean you only need to know a few.
A CD will change directories, make dir will create directories, and then LS will list directories.
Those are really the main Linux commands you need to understand.
So for example, if we're in our default directory and we want to create a directory maybe just to store
our C code and maybe we create a call it a project, we can say make dir a project and then we can do
LS.
And that will show us all of the different directories under our desktop here default directory.
And then to navigate inside there we can say CD project.
And we hit the tab button for tab completion.
And so once we're in a directory we're going to start editing our code.
We have to bring up a simple text editor.
And Linux does have a lot of various text editors that are available.
There are old school ones called VI.
You'll see me use this quite a bit, but it has kind of some commands that are a little tricky to insert
and add tabs.
So if you type in VI and we could say main dot c or whatever, it will create a file called main dot
c and the VI editor.
And it looks a little weird.
If you're familiar with VI commands, it's not too hard to use, but it's not the most intuitive.
You have to select I to insert, and then you can start writing your code and things like that.
And then you have to hit the colon or the escape key to go back to the commands, and then hit colon
and Q to quit.
So VI is a little bit harder to use, and so it's a little bit more complex.
The editor that I would suggest using is probably something like gedit.
That's a pretty simple one to use on Linux.
There's a lot of different ones.
And again, if you go to the show applications you can type in gedit.
And this will bring up a gedit text editor.
And it's a pretty simple text editor.
And so you type you run that and it will bring up the gedit text editor.
You can also incidentally bring it up from the command line.
Right.
You can say G edit.
And you can do the ampersand so that you still have the prompt.
And that will also bring up a gedit editor.
And this will bring up the editor in that directory project.
And so what you can do here is it's very easy interface to use to start creating your text, your C
source code.
And so what you can do is you can open an existing file if you already had one.
Or you can hit the plus to create a new document.
And here you can say you will see it will add tabs.
So we have a document right here.
Doesn't have anything in it, but we can save it anywhere we want.
And so I just created a directory under my desktop project so I can select desktop and say project.
And I'm going to put my file in there and I'm going to name it.
I'm just going to name it hello World.
Let's see.
You put it right up here in the name Hello World dot C and I'm going to hit save.
And then you can actually see that this file is now saved inside that directory by typing the LS command.
And you see hello world dot c.
So now we can go back back to our editor and you'll see it still listed on the left hand side.
And we can actually start writing our source code.
And again we haven't really gotten there yet where we understand all the source code.
But we're going to we're going to get there.
I know you're going to have a lot of questions about that, but the source code right now, we're trying
to set up our environment.
We're making sure we can compile and run.
And in this case we're compiling and running from a terminal.
So the source code is just a standard C template for a Hello world application.
We do the pound include.
We basically use standard I o so we can use some of the printf functions.
You can manually type this in.
And then we can just say int main.
Make sure you spell main correctly.
And we could have put a void in there.
And then a left curly brace and then a right curly brace.
And you'll notice that this simple text editor doesn't do a lot of the nice things that the IDE does
for you.
It doesn't do any of this code completion or add templated.
Code for you.
It doesn't add brackets.
Everything has to be done manually.
And so here we're just going to create our main function.
And we're going to do a print f.
And we're going to say hello world inside double quotes.
And we're going to put a line feed in there and it with a double quote right parentheses and a semicolon.
At this point we want to save it.
So we can either hit Ctrl S to save it.
And now you see some syntax highlighting happen.
The ant is in green.
The void is in green.
We can also just hit save.
Right.
And so here we just created a simple C program.
Right.
And this should look familiar from the previous videos.
It's just it's not it's not an IDE.
It's not in code light or anything like that.
It's in your own editor a simple G edit.
It doesn't have any of the overhead of projects or settings or anything like that.
It just has the templated code.
So now if you go back to the terminal and again, if you type in LHS, you see that Hello World dot
C exists.
We can actually see the contents of that to see if it was saved correctly by typing in the more command,
and then the name of the file hello world, and you'll see the source in there.
And at this point we've actually created the C source code.
But we haven't compiled it yet.
We haven't built it.
So we want to build it and then we want to run it.
Right.
And we talked in previous videos about how we installed the GCC compiler.
And so if you type in which gcc you'll see the path to that gcc compiler.
You can type in gcc dash dash version, you see the version of the compiler you're using.
And so in order to compile this you have to use that command the gcc command.
Now the GCC command can definitely get more complicated.
There's a lot of options to it.
There's all sorts of different things.
At the very basic level, you can compile a C source code by just typing in GCC and then the name of
the file in this case hello world dot c.
And you can always hit the tab button for tab completion.
So you don't have to type all of it in.
But if you just type in GCC, hello world dot C with no options, no flags, it's going to try to compile
your source code.
And so you can hit enter.
And if there's no errors it you'll see just the next prompt.
Now if you actually had an error.
So let's go back to our text editor.
Say we were missing a semicolon.
Any other common errors say we misspelled main.
We misspelled standard io.h.
We didn't have a curly brace beginning or ending, whatever the error may be, once we save that and
if we execute the command again, you'll see.
Now it says there's an error, right?
You're missing a semicolon essentially.
So you'd have to go back and edit it, add that semicolon, save it and then compile again.
Right.
And again this is very simple GCC command.
It can get a lot more complicated right.
So you can actually see a lot of the various options for GCC by typing in GCC dash dash help.
And this gives you a list of all the different commands you can execute with C.
So the common ones are the dash C the dash O for linking.
Again, I talk more about compile options in my advanced C course.
We're just trying to compile basics here.
So if we type in GCC and we have no errors on our file, you'll just see a command prompt that's returned.
Now you can actually.
And what you'll see as well is if you do an LS now you'll see a file that's created called a dot out.
This is actually your binary executable file.
And so you can actually run your program now by just typing dat dot forward, slash a dot out.
And this will run your program.
If you enter you see the output.
Hello world.
And so that's how you can compile and run a simple C program.
Now it can definitely get more advanced where you can have different compiler options.
You can change the name of the binary to something meaningful.
You can actually instead of hitting the dot forward slash to execute the executable.
So you could do something like DCC and then you can have the name of the file hello world and then dash
o to change the name of the executable.
So in this case maybe we'll name it something like my program.
And that will compile the code.
And it will change the name of the binary.
The program that you want to run.
You can ls.
And you'll see now there's my program.
In order to run that you do a dot forward slash and you say my program.
But again we're sticking to the basics.
I'm showing you how to compile a basic C program using a simple text editor, and then issuing the GCC
command in one line to compile the program, link it in one line, and create the executable program
that you can then run from the terminal.
So there's a lot to cover here.
Make sure you pause the video.
Go back.
But the gist of it is, is you can use a simple text editor, write a simple C program, write no ID
here, make sure it's saved.
Go back.
Confirm that it's saved your source code.
Compile it using GCC.
If you need help, use the dash dash help option, but basically creates a binary and you can run C
programs from the terminal.
Thanks.
And if you have any questions, please post them the forum.