Hamdi Ghorbel
3 min readFeb 5, 2020

--

What happens when you type gcc main.c !?

campilation of code

So how do we write all this code and computer knows exactly what we want. Well Compilation Process is the one that makes that happen and here is how it works.

The gcc command has many options that allow the computer to stop at any step. The respective options for the desired step is written in the caption below.

0.Create main.c file

main.c

Use any text editor to create this file.c, C is one of the top 10 most popular programming languages in the world. According to data from GitHub it is currently the #8 most popular language on all GitHub repositories.

1. Preprocessing

Preprocessing is where gcc looks for lines in the file that have hashes and interprets them. When we run

$ gcc main.c

Lines starting with a # character are interpreted by the preprocessor as preprocessor commands.This is the first stage of compilation process where preprocessor directives (macros and header files are most common) are expanded. To perform this step gcc executes the command internally.

2. Compilation

Next, the output of the preprocessor is passed into the compiler and the compiler generates assembly code. The assembly code generated is particular to the instruction set of the processor inside your computer. The most common of which are ARM and x86. The ability to utilize different assemblers enables gcc to turn C source code into machine code that can work on a plurality of computer architectures. Assembly code is the least human readable form of code before it becomes machine language, which is virtually impossible to read and interpret.

You can explicitly tell gcc by executing the following command:

$ gcc -C main.c

Takes the intermediary file and generates it into assembly code which is a low level language that translates will with the machine code of the computers architecture.

3. Assembly

The assembler accepts the output of the compiler and turns it into machine code. Machine code is just an executable binary file that contains instructions for the CPU (central processing unit) to interpret. The output of the assembler is put in a file called main.o . This output becomes the final input into what is called the linker in the compilation process.

Takes the assembly code and translates into binary code (object code) that the computer understands, we run with this command

4.Linker:

The last step before we are done with compilation is called linking. In linking, The linker accepts the main.o as input and it also accepts any pre-compiled libraries that were imported with the #include preprocessor directive. Next, it merges the unique (non duplicated parts) to make what is called a standalone executable binary. The linker should only link the functions out of a library that you declared. For example printf would be imported instead linking in every function, used or unused, from the stdio library. Since we did not use a compiler flag indicating otherwise and we did not use the -o flag in gcc , so the executable file will be saved to a.out (which is the default) in the current working directory.

$ gcc main.c
$ ls
$ main.c a.out

--

--