Hello and welcome back.
In this video, I'm going to show you how you can compile and run a C program via the Cygwin terminal
on Windows.
So our best option here is to compile and run through the Cygwin terminal, not the command prompt,
because we're using Cygwin as our compiler.
So in Windows, if you go down and you go into the search bar here, you can type in Cygwin Cygwin and
you should see a Cygwin 64 terminal option come up.
You can also, if you bring up Explorer, you can go to your cygwin path by going to your C programs
wherever you installed it.
I installed it in C Cygwin 64.
And then you can go into bin and you can see the terminal in there as well.
But once you bring up the terminal.
You should see a little sort of command prompt type thing come up.
So you just type Cygwin 64 and so you'll see this little terminal window show up with a prompt.
And what you'll notice it does have a path and so forth.
But the dollar sign is where your prompt is.
Now you can issue certain commands, Linux type commands in order to navigate and see see yourself around
here.
This environment, the basic ones are CD, which is a change directory and then LS, which lists comments.
So if we type in LZ right here, we can see I'll make this full screen, we can see everything that
is under our certain path wherever the default is.
So right now I'm in the cygwin C users J fedin directory.
So if I go back to Windows Explorer and I go to the actual Cygwin directory, you'll notice that the
directory structure has a home directory in it.
And this is usually for a particular user that you've created.
Usually by default, it will create whatever you.
Create it and it will be like mine as J.
Fadden.
Home j fitted.
Now, I would suggest you probably just put all your c source code files in c j Fadden because it looks
like a Linux environment with all these different directories, binary dev, Etsy and so forth.
So home is your user environment and so mine is j fed.
And inside there is where I'm going to put my C source code files.
And so if I'm back in the Cygwin prompt window, all I need to do is type in CD slash home slash J Fadden,
and that will get me to my root directory where I want to start saving my C code.
Now I'm going to create a directory using the make Dir command and I'm just going to call this projects
just to put in organize things better, and then I'm going to navigate inside there using saying CD
projects.
And at this point you can use any C editor that doesn't have like rich text format, anything that has
like a text plain text saved.
So things like Notepad Wordpad will save things as TFS.
You don't want to use Wordpad.
Notepad notepad plus plus is another good one that to use.
I'll just go with notepad just because it's a popular one.
So if I go and I type in notepad, it comes with windows.
Again, I usually use notepad plus plus we'll just go through notepad.
You can use any, any text editor that saves things as text file.
So if I bring up notepad, you know, hopefully you're familiar with this, but it's a simple text editor.
Let's make the preferences a little bit bigger for our text, make the font a little bit bigger so we
can see what's going on.
We'll say 16.
And so now we just want to save this to our home fund projects directory.
So if we go into file and we say save as we can just navigate to that directory where we installed Cygwin,
right?
So again, it should be in C cygwin 64 home and then the username Jay Fedden and then projects.
So here I'm just going to create a file and I'm just going to say all files as my type and I'm going
to just call this Hello world dot C and I'm going to hit save.
And so now if we go back to the Cygwin terminal and we issue the LS command, you'll see there's a file
there saved called Helloworld dot C.
And we can actually look at the content of that by seeing more command.
Hello, world.
See?
And that tells us there's nothing inside there yet.
So the CD and the Makedir command and the LS command are really the only three commands you need to
get going in order to just navigate around and see where some of your source code is.
So now I can go back to Notepad and I can start writing my C code.
Now I know you're probably gonna have a lot of questions at this point.
What is this code that's not important right now?
We're just trying to set up the environment so that we can run and compile our C code.
In this case, specifically from the terminal, we don't have to use an IDE.
So I'm just going to create some C code here.
Include is including a standard i o header file inside the angle brackets.
I'm going to say int main.
And this all has to be spelled completely correctly.
I'll say void.
I'll use the left curly brace and I'll have the right curly brace.
The one thing you'll notice is in the IDE, a lot of this code completion will happen for you.
It will automatically add brackets.
It may correct misspellings and so forth.
And it will also usually highlight the syntax in different colors.
So there are some advantages of ides.
Now you have to make sure everything is spelled correctly.
Lowercase I main or this is not going to compile correctly, just like in our previous videos.
Here, I'm just going to say printf.
I'm going to say hello World Inside double quotes make a space.
Say backslash n for a line feed and the double quote and the right parentheses add a semicolon.
This is C code.
Has to be exact.
It can't misspell main can't misspell standard.
I'll use the brackets.
Double check this.
Use the pound.
Make sure you pause this double check it.
Now we can hit file.
We can hit save.
And again, we can double check that everything's saved by just using the more command.
And now you see content for that file.
Now we're going to use the GCC command to compile our source code.
Now, before we checked, if GCC was installed, you can actually type in which GCC will tell you where
the path is.
You can type in GCC dash dash version and we'll show you the version of GCC that you're using.
And this is a command that's going to compile your C source code.
Now, I'm only going to show you the basics of the GCC command.
It can definitely get more complicated if you're doing different settings, preferences and so forth.
I want to get you up and started just so you can compile and run a C program.
So in order to run a simple C program, you can just say GCC the name of the source file, which in
this case is Hello world dot C, You have to make sure you're in the right directory of the source file
and then you can just hit enter.
And if there was no errors in your code, you'll see the prompt come back immediately and the dollar
sign or whatever the prompt is.
If you type in LHS, what you'll notice is there's a new file that was created called a dot exe.
That's the program that was created since there were no compile errors in order to execute this program,
you can just say dot forward slash dot x and this will run your program.
And you'll notice when I type that and enter it, it says Hello world, which is the output of the source
code.
Now if I had an error in my source code.
So let's say I go back, I forget a semicolon or I misspell main or I forget a curly brace, whatever
the case may be.
If this is now saved and I hit Ctrl s to save, I go back and I recompile.
Type in GCC Hello world C What you'll notice is there's an error.
I'm missing a semicolon and no program should have been generated.
It may, you may still have the old one in there, a dot x but no program was generated because the
compilation and the build failed, so we would have to go back, add the semicolon, save the file,
recompile, and now we can actually and you can actually see that the the time a dot x it was just generated.
And then we can run the code again.
A dot x.
Now, those are very basic compilation commands, basically just GCC with the source name.
You'll notice it created a default name for your binary, your executable a dot x.
Now, if you wanted to change the name of the x to be something meaningful, you can actually say GCC
name of the file.
Hello world dot C.
And we can say dash o and then we can change the name of our binaries.
We can do something meaningful, like maybe, say, my program.
And again, if there are no compiler errors and you hit enter and you do an LS, you're going to see
that there's a new executable called my program dot X.
And then we can execute that.
My program dot x.
And again, GCC gets much more complicated with options to the command line.
If you type in GCC dash dash help.
You can see some of those options.
The real important ones I already mentioned the dash O the dash C will just do a compiler a compile
but not link.
So you kind of if you don't leave that by default, it will do compilation and linking in one step.
For now, the dash o is really the main one that you want to focus on.
Again, you can get a little more complicated if you do GCC.
You can type in the dash dash, STD for the version and you can say like something like C 99.
And then the name of the source file, if you want to use a specific version of C, and this one actually
says so I must have typed in something wrong.
This may be C, it's a lowercase E, not an uppercase C, right?
So there's a lot of different compiler command line options that you can send, but that's for another
time.
That's for more advanced options.
When you compile, this will get you up and running.
If you don't like an IDE and you just want to compile from the command line, very simple to do.
In Windows, you go to the Cygwin terminal, you navigate to the right directory, you create a simple
C source file with a C extension and a simple editor like Wordpad or Notepad plus plus you save the
content of that file.
You issue the GCC command, which creates a binary, and you run that program by just saying dot forward
slash the name of the program.
Now, if you have any questions on this, please post them in the Q&A.
Thanks for listening.