The steps of compilation

Erika Osorio Guerrero
3 min readJan 26, 2021

What is C

C is a computer programming language and was originally designed for and implemented on the UNIX operating system.

It is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute).

With C you can create lists of instructions for a computer to follow.

What is compilation

Compilation is basically the translator of what we write (human code) to the language of computers.

The Free Software Foundation GNU project developed the GNU Compiler Collection (GCC) which provides a core capability to support multiple languages mainly C and C++

syntax

gcc [-c|-S|-E] [-std=standard]

The Compilation process have 4 steps

Step 1: Preprocessor

The C compilation begins with pre-processing of source file. Pre-processor is a small software that accepts C source file and performs tasks like: removing comments from the source code, expansion of included header files. The preprocessor just reads the code source. Let’s see an example of the process with the file →helsinki.c

This is the Command to preprocces the code:

~$ gcc -E helsinki.c

the filename will be have the filename: helsinki.i

Step 2: Compiler

The preprocessed code is translated to assembly instructions specific to the target processor architecture. These form an intermediate human readable language. The compiler will Check C program for syntax errors., translate the file into intermediate code like in assembly language and Optionally optimize the translated code for better performance.

This is the Command to preprocces and compile the code:

~$ gcc -S helsinki.c

→ the filename will be have the filename: helsinki.s

Step 3: Assembler

is used to translate the assembly instructions to object code.

Assembler accepts the compiled source code and translates to low level machine code. After successful assembling it generates filen_ame.o (in Linux) or file_name.obj (in Windows) file known as object file. In our case it generates the helsinki.o file.

This is the Command to processes, compile and assembly the code:

~$ gcc -c helsinki.c

→ the filename will be have the name: helsinki.o

Step 4: Linker

Finally, the linker comes in action and performs the final task of compilation process. It accepts the intermediate file <file-name.o> generated by the assembler. It links all the function calls with their original definition. Which means the function printf() gets linked to its original definition.

Linker generates the final executable file (.exe in windows).

Finally the command to compile the file helsinki

~$ gcc -o helsinki  

→the filename will be a.out if you don’t give a name

~$ gcc -o helsinki helsinki.c

will have for name helsinki

--

--