The Tools and Libraries

Before we start learning C language, let’s first take a look at the tools needed. We already know that C language is a ‘Compile to Run’ language, after we write our program, or rather, source code for the program, we need a ‘compiler’ to compile and turn the source to runnable program. On Linux, we’ll be using gcc, GNU Compiler Collection. gcc does more than code compiling, but it is good enough for now to just remember that gcc is the magic tool to turn our C source code to real program.

For example, if we already write our program source in a file called “hello.c”, we can use gcc to to ‘compile’ it,

gcc hello.c -o hey

This will create a runnable program called ‘hey’. If ‘hey’ has problems, we can edit the ‘hello.c’ file then run “gcc hello.c -o hey” to generate a newer version of ‘hey’ program.

Sounds straight forward?

There’s more.

Invoking gcc manually each time like the above is ok, if our program is simple and only has one or a few source files. Imagine a complex program has thousands of different source files, we’d need some other tools to manage the way ‘gcc’ is called. One of the most popular tools is called ‘make’. We’ll not learn ‘make’ here, but this is something to keep in mind. Once our program grows beyond a few files, we’ll need tools like ‘make’ to manage how ‘gcc’ is called. For even more complex program, there are tools built on top of ‘make’ to further simplify the ‘compile’ process, such as automake and cmake.

Other than ‘gcc’, there’s also the library part.

Programming language is designed so that we can program to give working instructions to CPU. However, it will be a problem if every programmer needs to write everything from scratch. For example, in order to create a file on disk, we need to be able to access the disk itself, find the available directory, open a file, write to it, and save, then close. Each programmer probably would write the same thing over and over to handle the disk access and file open/write/save/close part. Ideally the code can be shared so once it is written by one programmer, the other programmers can just reuse it.

One of the mechanism for reusable code is called ‘Library’. Think ‘library’ is a collection of shared code, that can be used by any programmers.

‘gcc’ plus ‘C library’, this will give us the base to C programming. To put it all together, when we program, we write our program following C language grammar, and using shared code from library.

Ok, enough talking, let’s see how to use ‘gcc’ and how to use code from library, open the editor, I am impressed and pleased to see that Frank actually learned to use emacs already! use your favourite editor to create a file named “hello.c” with the following lines,

Now, type the following,

gcc hello.c -o hey

If you see something printed on your terminal, something went wrong, double check to make sure there’s no typo. If nothing happens, that’s good. Remember that “no news is good news”? that means our compiling succeeded. You now should see a file named ‘hey’ is generated. ‘ls -l hey’ even shows that file is runnable!

Let’s run it,

./hey

You see ‘hello’ printed on terminal, it works! We just compiled our first C program!

We used the ‘gcc’ tool, but where is the library?

In this simple example, we only used the standard input/output library, for ‘printf’ support, that’s what the “include <stdio.h>” is for.  By including that, ‘gcc’ automatically finds shared code from that standard library. There is a lot more to learn on how to work with libraries, once we learned the language. For now, it is good enough to know that including <stdio.h> is sufficient to bring in the standard shared code.

Now, we learned all the programming basics, we finally are ready to learn the programming language itself …

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">