What is GCC, what processes does it have and what happens when you type gcc main.c?

Miguel Enrique Grillo Orellana
5 min readOct 2, 2021

GCC stands for “GNU Compiler Collection” is the tool that allows us to compile one or more files. GCC is a built-in compiler distribution for several major programming languages.

Ubuntu 20.04 LTS was used for the creation of this image.

To understand this, we must first know how the basics of gcc and its build processes work. Compiling a file requires it to go through 4 processes. These processes are each called a preprocessor, compiler, assembler, and linker.
Let’s start by creating a
main.c file that prints the “hello world”.

“For these examples, the coding style is primarily inspired by the Linux kernel coding style and the C89 style.”

Ubuntu 20.04 LTS was used for the creation of this image.

I will do some examples explaining the process, but first I will add that gcc has an option that is -o with this option we can name the executable file with another name that is required. For example, gcc main.c -o main, then the resulting non-replaceable file will be main and no longer main.c, so we would have two files, one main.c and the other main.

Ubuntu 20.04 LTS was used for the creation of this image.

As we can see in the image, the compiled main has execution permissions for both Owner, Group and Others.

It is as if we had changed the name to main with mv main.c main and then granted execution permissions with chmod a + x main, but if we look at the size (in bytes) it is totally different, since it we are looking at is a fully compiled file.

Now after explaining the compiled file renaming process, I will proceed to explain the processes.

PREPROCESSOR

The preprocessor removes comments and includes header files in the source code, replaces the macro name with the code.
Code:
gcc -E main.c

Ubuntu 20.04 LTS was used for the creation of this image.

Here we have the code, but the entire preprocessor is printed without storing it in one place, but with what has already been explained above, we better save it in a main.

Ubuntu 20.04 LTS was used for the creation of this image.

As we can see, it is not an executable and when entering the file it will only show us this.

Ubuntu 20.04 LTS was used for the creation of this image.

COMPILER

The compiler generates assembly code.
Code:
gcc -c main

Ubuntu 20.04 LTS was used for the creation of this image.

Here we have main.c, but without the need to change the name, a file named the same is added, but with a “.o extension. Within this file we will find the following.

Ubuntu 20.04 LTS was used for the creation of this image.

ASSEMBLER

The Assembler convert assembly code into object code.

Code: gcc -S main.c

Ubuntu 20.04 LTS was used for the creation of this image.

Here we have main.c and in the same way, without having to change its name, a file with the same name is added, but this time with the extension “.s”. Within this file we will find the following.

Ubuntu 20.04 LTS was used for the creation of this image.

LINKER

The linker takes the objects generated in the first steps of the compilation process, the information of all the necessary resources (library), removes those resources that it does not need and links the object code with its library (s) with what finally produces a file executable or a library.

Now we will compile the file completely and call it “hello_world.

Code: gcc main -o hello_world

Ubuntu 20.04 LTS was used for the creation of this image.

We have created the executable file “hello_world”.
As it is an executable inside the file we will find this.

Ubuntu 20.04 LTS was used for the creation of this image.

But if we run it, it will put what we really want in the output of the code made in main.c.

We execute with “./hello_world”.

Ubuntu 20.04 LTS was used for the creation of this image.

What happens when you type gcc main.c?
If we compile a file directly with
gcc main.c, it will result in an executable a.out, since by not assigning it a name, it compiles by default regardless of the initial name of the file.
Code:
gcc main.c

Ubuntu 20.04 LTS was used for the creation of this image.

When executing it, it will give us the same as if it had a name.

Ubuntu 20.04 LTS was used for the creation of this image.

In summary

All this is very important because the application of each of these codes will give us each different type of process in the gcc, which helps us to be able to enter each type of process in its compilation.

Ubuntu 20.04 LTS was used for the creation of this image.

--

--