Beginners Programming with Nonso

If you want to know more about compilers, then it is safe to assume that you know a a little bit about programming. Then I hope you do understand that humans and machines in as much they work very well together are very different. One of those differences is in the language they speak. While humans speak in alphabets, machines speak in numbers. So the question is how do we communicate with the freaking machine.

This is where a compiler comes in.

Who/What is a compiler

A compiler is this guy below.

african translator

A Human Translator

In that note we can say a compiler is basically the guy who translates our instructions (program) into a language for the computer to understand. If you want to learn more what language the computer speaks then go here. Therefore it is safe to say compilers are basically translators like that guy in the image above.

Now depending on what Programming English you’re speaking (Java, Js, C, C++, Perl (habour), Ruby&Rails, Python), each language have what they call their translators. Some call it compilers, others call it assemblers or interpreters, but they are all doing the same thing which is translating to machine language albeit in a slightly different way.

 

How does C compiler works

If you look at it from another perspective, a compiler converts your code to machine code. In other to do this effectively, there are certain things the compiler expects you to have done on your code and also there are certain things the compiler does on its own.

After you have your program ready and sent to the compiler, the first thing your compiler does is to check if it is even a file it can compile. Since we are using C language here, the compiler checks for a file within the folder you’re compiling from for a file ending with the extension .c. So you want to make sure the file you are asking to be compiled ends with the .c extension. This file is popularly known as the source code or source program.

c-file

After this, there are 4 steps a compiler then takes to get your code compiled.

Step 1

1) Preprocessing

Like the name suggests, the compile at this stage, prepares your script for processing. During this step the compiler does 3 major things –

– it removes all comments (the machine don’t care what you intend to do with your code.)

comment

– includes all the external files as indicated in the code and macro expansions. That is those lines of code starting with ‘#’. It basically gets the file that has been included, copy all the content on there and add it to the source code/program.

include

After pre-processing, the compiler produces a file with a .i extension. Usually your filename.i, in my case I will get a main.i. It then sends this file to the second step.

 

STEP 2

2) Compiling

Yeah I know it feels like the real name but this is just a one function of the whole process. During this step, the compiler compiles(converts) the file into an assembly file. Assembly file are low-level codes. They are not machine code as it still contains some form of English. But it is during this stage that all the errors (mostly syntax errors) are checked for. If errors are found, then your code is not going to compile. I guess you see why it is called the compiling stage?

After compiling without errors, a file is produced with a .s extension which is known as the Assembly file. In my case that file will be the main.s file.

 

STEP 3

3) Assembling

During this step, your assembly file is then converted to the machine code. So assemblers actually converts your code to machine code. The by product is a file with a .o extension. This file is known as the object file.

In my case, my file will be main.o

 

STEP 4

4) Linking

Linking is the final process. The linker adds all C library files to our program files. The library files are basically what contains the definitions of all your pre-defined functions you used in your code. These are C files and the linker links these system C files with your own object files. Also if there are multiple files, the linker links everything into one file.

At the end of this process, an executable file is created with extension of .exe in DOS and .out in UNIX OS. Under Windows, the executable file usually has the same name as the source file, with the c extension replaced by an exe extension.

The whole story in one picture

compiler-process

Understanding the Compiler Commands in Linux

You will need to install a compiler for C. If you need help on how to install compilers read this article. Compilers does not come shipped with some operating systems. To know whether you have installed a compiler, open your terminal and type the following below:

gcc --version

gcc – is what is known as a command in programming which is in turn basically a program that tells a computer what to do. Compilers comes with additional options that goes with the command. For example if you want to compile only using step 1, you can use the command below:

gcc-e

Also if you want to run a comprehensive compilations, you run the code below

gcc

The code above can be interpreted as

gcc : (the name of compiler) and the command the starts the compilation.
-g : (allow debugging)
-pedantic : (only allow real C)
-Wall : (provide all warnings of possible mistakes)
file.C : the source code/program

For other options, use the command below

gcc --help

Compiler versions

Sometimes you will be surprised to receive errors that are based on the versions of your compiler. So just like any software, compilers have versions. Below are some of the versions of compilers –

C89/90, a version of C once standardized,
C99 replaced C89 and C90 in 1999.
C11 replaced C99 in 2011.
C17 replaced C11 in 2018.
C2X will replace C17 in 2023.

These versions are slightly different in terms of certain convetions such as the way variables are written etc.. but this should not hinder you at all. Popular versions are th C99 or C11.

 

Over to You
———–
Now you know better, to whom much is given, much is expected. Compilers are simply translators. It follows 4 steps (pre-processing, compiling, assembling, and linking) in producing an output. Thanks for reading and do let me know if you enjoyed the article.