Photo by Markus Spiske on Unsplash

What happens when you type gcc main.c

javier gutierrez

--

To have a better understanding of what gcc does it is important to explain how a computer works. Computers don’t with the same language as us humans do, they have their own language which is the code we type into the computer, this codes then has to be transformed into a binary language which is the language computers understand. This information is later transformed into human readable information or in other words a executable file.

Compiler process

The chart above explains in greater detail how the transformation of this language in each step takes place.

But to make it even clearer let’s try and explain it in more detail. For starters we have to create the main.c file, file that is created in c language and in which inside is a code that will at the end provide us with an executable file.

Once we have created a file (.c) it will go through a compilation process in other word make our code readable or understood by the machine by converting it into binary code, binary code being zeros and ones.

It’s here where the compiler comes in. We run our file, (the one created earlier) main.c and run it with gcc, by typing first the compiler followed by gcc in the shell.

gcc to compile main.c

What exactly did it happen here?

GCC run the code through the steps shown in the first figure.

It first went by the Preprocessor, wich prepares the file by removing comments and including codes from header and sorce files.

Then it goes by the Compiler, here all the code and information sorted by the preprocessor is assembled.

In the Assembly process, the code generated by the compiler is turned into binary code, so the machine can interpret.

Linking is the final process. After the assembly process has finished and the machine is able to read our code (in binary) and it has been fed with information from the respective libraries it generate an executable file in form of an a.out file or however you name it when generating the executable file.

--

--