What happens when you type gcc main.c?

Diana Henao
3 min readFeb 10, 2021

--

Photo by Markus Spiske on Unsplash

What is gcc? gcc is a compiler. Its name comes from the acronym in English GNU Compiler Collection. In general, a compiler is a program that allows you to translate code written in a programming language into a language that can be read and processed by the computer, called a low-level language (such as machine language).

The compilation process encompasses four general steps: pre-processing, compilation, assembly, and linking. Let’s see each of them:

1.Processing: In general terms, in preprocessing, comments are eliminated. Additionally, the directives that have been given to the program are interpreted (those identified with the #). These directives can be of the #include type to include declarations from a specific file. It can also be of type #define, used to define a constant. With the -E option in gcc it is possible to get the result of the pre-processor step:

-E option -> pre-processor step

2.Compiled: In this step, the code written in the favorite programming language is translated into assembly language, a language considered low-level. With the -S option it is possible to stop before the assembly is generated:

-S option -> before assembly is generated

3.Assembly: In this step, the assembly code is transformed into machine code. A file with a .o extension is generated. In this file, the syntax and semantics of the code have already been verified. Here is where the source code is interpreted, and the object code is generated. With the -c option in gcc it is possible to obtain the result of the compilation, before linking:

-c option -> compilation before linking

4.Linking: Finally, all the necessary libraries are integrated, included within the code. That is, those functions that are part of libraries or external references are included. For example, the function printf () that can be used in the code, with the inclusion of the directive #include <stdio.h>.

As you see, by using gcc as a compiler for a .c file you can drill down through the steps (with different options or flags) to see what is happening. But in the end, it is only necessary to run a general command to compile a file.

A general command used to compile a C file and creates an executable file

Finally, I leave a graphic summary for a better interpretation:

Summary of gcc compilation process :)

References:

--

--

Diana Henao

Programmer in training, because there is always a lot to learn. I’m not the best, but I’m trying my best!