lesson 02- Setting up the Environment in C
![]() |
setting up the environment in C |
Part 2: Setting up the Enviroment
Before we can start programming,
we obviosuly have to have the requiered programs to start programming right?
This section will cover the required software to begin following along. It will
also cover some of the basics of how a C program is made.
Creating a program in C
Before I
cover the requiered tools to start writing code, I briefly want to touch on how
a program is even made in the first place. Doing this
will help you to have more of an understanding of why we have the tools that we
have. BEFORE WE GO ANY FURTHUR, all of these tutorials will be using Windows,
if you want to follow along on Linux or some other platform, most of the code
will work fine (minus any Win32 stuff we use way later on) but the process of
converting your code to an executable will be different, so you need to keep
that in mind as we go through these tutorials.
Compilation, Executables, and Other Stuff.
Alright,so lests begin.
Let's start with a program is. Whenever you
run a program on your computer, a lot is actually happening, and you can get
into pretty low level minutia about how all of that works, so instead of
getting crazy technical, I'll do a more broad overview. So when you first open
a program, a program called the "Kernel",
loads the program into memory. When the program gets loaded, there are
typically different dependencies that it has, such as a library it needs to get
data from, etc. These libraries are called "DLL's". The kernel goes
through and finds the dll's the program needs to run and allocates memory for
those along with the main program. Now we havn't covered functions yet, so I'll
try to word this differently, but essentially your main program will typically
make calls to various sections of code within each dll. The kernel goes through
and it stuffs in the memory addresses of each of those functions contained
within the dll into your actual program memory. This is just the basics of how
an executable actually get's started; the actual running of the program will
depend on the language/compiler used/etc. So how do you get a program in the
first place? Well, you write some code and compile it.
Compilation; (compiling) involves taking a set of ASCII
text (ASCII is a form of character encoding, or if that makes no sense, think
of it as a specification for how to convert binary to readable characters,
ASCII is a version of this) that then get's fed into a "Compiler".
The compilers job is to take this code and produce an executable by reading in
the code you wrote. This essentially converts the code down to assembly that
runs on the chipset you are on, and it does a slew of other things as well that
are outside of the scope of how deep I'm going to get. At this point you may be
asking something like "Well, where does C come in?", and that's a
valid question. Here's the thing, for all intents and purposes, there is no such
thing as a "C language", there is merely a compiler. See, what
happens is when you write a language, you are actually writing a program
(compiler) that will read characters in certain sequences and spit out assembly
accordingly. Now, for this reason, there is an organization called ANSI, which
is the American National Standards Institute. These are the people in charge of
setting the specification of the C language (among other languages), which will
tell anybody who ships a C compiler how their compiler must function. You may
come across the terms ANSI C, C99, etc. These are standards of the C
language.
So what do I need?
I thought you would never
ask! To begin programming is quite trivial
actually, you will need two programs and a computer that is operational to some
degree (I'll assume you have the third). The first program you will need is
some sort of text editor, you can use literally anything, but I'm going to make
a few suggestions:
NotePad++ Is a pretty powerful text editor that is
completely free, it offers some syntax highlighting and intellisense (if you
don't know what that means, just know that it's really useful.) Download
Link Here
Atom is a free text editor distributed by GitHub,
it is a pretty decent little editor and it has some nice features, I'm actually
writing this as we speak in Atom (I'm playing around with it). It's hackable to
the core and it has a lot of community backing it and creating add ons for it.
Download Link Here
Visual Code is Microsofts response to open source
text editors, it's not a bad editor, and it is open source and cross platform (as
are the other two) so it's not too shabby. It is still in beta, so it's a bit
buggy at the moment, but still totally useable (that said, it's at version
0.8.0, so by the time this is released it may be out of beta...), I enjoyed
writing code with it. Download Link Here
There are several other editors you can use, such
as Emacs and VIM, I just think that these editors have a very high learning
curve so I'm not going to recommend them to people who have never even
programmed before, because as a beginner you won't get much use out of the
advanced features anyways.
The; final thing you will need is a C compiler. Again,
we are using windows, so if you are on another platform, you will need to find
another compiler. As an aside, all of our early on compilation will be via the
command line just so people are aware. I feel that learning command line first
is a very good idea and it is generally just a better way of compiling code
honestly, and not just for C; alot of things in say C# are best done with the
command line, so if you have experience doing compilation with the command line
you will be in good shape. Anyways! so, sorry in advance to any Visual Studio
haters, but it is the compiler of choice on Windows so...suck it up. We'll be
using the Visual Studio Visual C++ compiler. But wait, I thought that this was
a C tutorial?? It is, C++ is C with object orientation added on, and so if you
put C code through a C++ compiler, it will compile properly, which means you
can easily learn C++ once you know C (Add that to perks of learning C).
Anyways, go through the steps of getting it downloaded and we can move on to
the annoying part of getting set up, which is getting the command line ready to
be used. Here is the link to the download, notice this is a link to the download for Visual Studio, this is because
Visual Studio comes with the compilers you want (C#, F#, C++, Basic (not that
you'd ever want to touch that) etc.) and plus we will be using Visual Studio
for debugging purposes eventually and hey, I may even show you guys how to use
Visual Studio for compiling code as well, but that will be later on, so you may
as well download it now. Another thing, this is the community edition, Visual
Studio comes in different versions, so Community is a free version of Visual
Studio that is a bit better than the regular free version, but with less
features than the ultimate edition (very expensive). Now at the time of writing
this, this version is still free, if it is not, go and download the express
version of Visual Studio, it will work just fine.
Setting up the command line
First order of business, you need to figure out where your command prompt
is and put it somewhere accessible, I have mine on my taskbar so it's always
right there. Open it and take a good look at it, very bland right? Boring,
un-inspired, dull, stab my eyes out ugly. Fortunately, we can make this look a
little nicer. Right click the command prompt icon and click properties, you
will have two tabs you can play around with that will let you really customize
the look, the font tab and the colors tab. These will help you want to kill
yourself a little less while looking at your command prompt. This is how mine looks if you are interested, I am
using Red 235, Green 140, and blue 80 for the text color, and size 16 Consolas
font with bold checked on.
Next; up, we need to learn how to actually invoke the
compiler, afterall being able to use it is pretty important. Now I could go on
for a while as to why this is how it is, but I'm going to spare you guys the
rant and just leave it at this: As little sense as it makes as to why Microsoft
sets up the compiler the way that they do, there are some historical reasons as
to why it is how it is...you'll just have to take my word for it. With that
warning, let's get to it. Before I teach you how to set it up you'll need to
know a few commands to actually navigate, the main two being "cd" and
"dir", typing dir will tell you what files are located in your
current working directory, so for example, if I am in the C drive and I type
dir, I get the result in the picture from earlier. The other command is
"cd", this command is how you navigate to a new directory. To do
this, you type "cd (filename)", so for example if I want to navigate
to program files from my C drive I would type "cd Program Files".
Note that these file names are not case sensitive, this is a Windows thing,
unless I am in error I believe Linux and Mac OS are case sensitive so keep that
in mind. Lastly, if you want to navigate backwards you type "cd ..",
and if you want to navigate back multiple files you type "cd
../../../", each slash signifying the number of directories you wish to go
back.
Alright;, now we are ready proper to get the compiler
set up. First off, you will need to locate where you downloaded Visual Studio.
In general, it should be under the path name "C:\Program Files
(x86)\Microsoft Visual Studio 12.0\vc", however I can't guarentee this,
but it will for sure be in the Microsoft Visual Studio (version number) folder.
The vc part of that file name is the actual Visual C++ compiler folder. Within
this folder is something called a bat file. A bat file is (usually) a simple
script that you run to do something. Now, when you get to this directory, type
dir and you should see a file called vcvarsall.bat. This is the important file
we need. Essentially what this file is going to do for us is let us use the C++
compiler no matter what directory we are in. This is one of those moments you
wonder why they didn't just have this run everytime the command prompt is
opened, but I digress. Now, type the following command "vcvarsall.bat
x64". What this is going to do is set up the 64 bit version of the
compiler, essentially you can pass what's called a flag to the bat file, flags
in terms of the command line just mean you turn on or off certain settings. If
you simply ran the bat file I believe it would default to the 32 bit version of
the compiler, don't quote me on that. Now I want you to close the command
prompt and re-open it..................Did you do it.....?
Great, so heres the fun part about Microsoft, that bat file only lasts for the
the duration of the command prompt session you are in, so when you close it,
you have to set all of that stuff up again. fun right??
What; we are going to do is create our own bat file,
and we're going to call this bat file "Startup.bat", this will be a very
simple bat file that will simply run when we open the command prompt, and it
will call vcvarsall.bat so that every time we open the command prompt, it sets
up the C++ compiler by default. I recommend putting this file in your C drive,
but do whatever you want, just make sure you know where it is at. Open a new
notepad document and type the following:
@echo off
call
"C:\Program Files
(x86)\Microsoft Visual Studio 12.0\vc\vcvarsall.bat" x64
Obviously if you're path name is different change the directory. Now, save this
to wherever you want as "StartUp.bat". Now, right click the command
prompt icon and click properties. There will be a section under the shortcut
tab that says "target". put this in there
%windir%\system32\cmd.exe /k "c:\startup.bat"
Again, the directory should be changed to whatever you chose for it to be. So
this will set up the compiler every single time we run the command prompt. If
you would like to, you can also set the default location to start in by
specifying the path of a certain folder in "start in", this means
when you open the command prompt it will set up the compiler for you and put
you into the directory you are doing most of your work in, which is pretty dang
nice if I do say so myself.
A Few Last Things
I know this has been a lot
of work just to set up a stupid compiler, but I promise it's well worth it,
plus we're almost done! Alright,
create a folder for the code examples we'll be doing and name it whatever you
want. This is the folder I've set my start location to, obviously you can do
whatever you want but it would probably be a good idea to do this as well. In
this folder, I want you to create a folder for each lesson we do, this will
force you to modify the bat file and keep the build process fresh in your mind
as we go through each tutorial. I've created a folder called Lesson1 for when
we do our first programming lesson. In each lesson folder, you will follow the
same format, a folder called "code" and a folder called
"build", the code folder will hold any source code and our build bat
file. The build folder will hold our actual output files from each build,
including debug information and the actual executable. Now, the only thing you
should need to change for each lesson is the folder the bat file is
referencing. So, in your code folder for Lesson 1, open notepad, and copy this
(Make sure that everything is on the correct line, I've identified where to
start a new line with a * character)
@echo off*
*
mkdir
..\build*
pushd
..\build*
cl -Zi c:\CWebsiteTutorials\Lesson1\code\main.cpp
user32.lib
gdi32.lib*
popd*
So this looks confusing, but it's not. Line 2 says "if there is no
directory called build, make a directory called build". Next line 3 says
"Now, go to this build folder as the current working directory." Line
4 uses a command called cl. This is the command you use to invoke the C++
compiler, so whatever file you type after is the file it will compile. The
user32.lib and gdi32.lib are libraries that I've decided to include for when we
do Win32 later on. GDI is how you do graphical stuff in C. You'll also notice
I've passed a flag (whenever something has a - next to it, it's a compiler
switch, meaning a setting you can turn on or off), this flag is Zi, and it
means give me debug information. if you don't want debug information, you
simply don't use the compiler switch. Important side note, MAKE SURE that
you're file names don't have spaces in them, it will screw up the compiler when
it tries to find your file. So name your file Lesson1. Now, save this file as
Build.bat in your code folder, and open the command prompt and navigate to the
code folder. In this code folder, add a new notepad document and type the
following code (And yes, I do mean type it, don't copy and paste, get used to
typing things like a programmer, because progammers have a whole different set
of keys we care about).
#include <stdio.h;>
int main()
{
printf("Hello World!");
getchar();
getchar();
return 0;
}
This is what we refer to as a "Hello World" program, it's the typical
first program. Save this file as "Main.cpp", and yes, name it exactly
that or you'll need to modify the bat file. Now, using the command prompt and
while you are in the code folder, type "build"; this will run your build.bat
file for you. You should see a bunch of stuff pop up and then nothing. Back out
to your build folder and type dir, the thing you care about is
"main.exe". Congratulations, you have compiled your first C program.
If you type main, it will run the program in the command prompt. It should
output "Hello World", and wait for you to press a key twice. Alright,
that's it, your enviroment is now completely and utterly set up, you will never
ever have to redo this ( unless you get a new computer or wipe your hard drive
or something like that. ). The only modifications you will ever need to make
are when you create new folders for each lesson you'll have to edit the
built.bat file to point to the new directory. I will explain this for the first
few lessons but then it's up to you to remember to do it.
Conclusion
So, I know this was a LOT
of work, it probably was very tedious, but hopefully my
instructions all made good sense and you didn't have a bunch of weird errors
pop up. Think about this though, you've gone from (possibly) knowing nothing
about programming or the command line to understanding how to set up your own
build system to compile programs, bat files, how the command line works, how
the C++ compiler works at some level, how the OS handles loading your
executable, and a few other things. Not bad for just setting up your enviroment
I'd say. In the next lesson, we will disect the program you wrote and learn
about what each thing is and how it works.
Lesson1: A briefoverview of the Language: Lesson3: Hello World in C
Technology that is needed in all the world and in the future, is the technology. whose more than knowledge about technology. The more progress he can make. And it is necessary to have knowledge of every human technology. Because now everything is through technology. You can find help on galido.net for any kind of technology related information. Here are all information about all types of technology and tips.
ReplyDeleteClick here to know more information Tech Blogs
This comment has been removed by a blog administrator.
ReplyDeleteAre you looking for the best way to exchange coins? I am a cryptocurrency trader and investor since 2015 and know very well about all the sites. Do you know about Doge clicker Brendon? I think you don't know about this. You can visit this site to get to know about this.
ReplyDelete