The compilation process in C: what happens when you type gcc main.c?

Every time, after you are done programming something in C, you save your file with a .c extension, then, in order to have an expected output, you need to compile your file using gcc, what happens in the meantime is not simple, a process occurs there, this is know as the compilation process. Example: gcc main .c

The compilation process is produced through the GCC. The GCC or GNU Compiler Collection is a compiler system produced by the GNU Project which supports various programming languages such as C, C++, Objective-C, Fortran, etc. GCC is a very important component of the GNU and it is the standard compiler for most Unix-like operating systems.

So, when you compile a .c file using gcc this means that the system coverts the source code written in a text editor which is in “human language” into object code or “machine language”, this happens every time you run your code if it doesn’t have any error.

The compilation occurs in 4 stages: preprocessor, compilation, aseembly and linking. The first step takes the source code of the original .c file and removes the comments (because they are necesary only for us) and includes the header files, this is: identifies the preprocessor directives, which have the # character and interprets them. For instance, if the file has stdio.h (see the previous picture) in the header where # is, the preprocessor looks for the avaliability of that directive, once is found it, it is replaced with the content of the header file stdio.h. If you decide to stop the process here, you should run: gcc -E file name, this will be stored in a file with extension .i.

In the second stage, the “prepoccesed code” (without comments) is converted into assembly code, this is like an intermediate language (not human, not machine) used in C. If you decide to stop the process here, you should run: gcc -s file name, this will be stored in a file with extension .s. The third stage takes the assembly code and converts it into object code or the language a machine can understand, which is binary language; the file generated is stored with an .o extension.

IN the last stage, the process looks for the libraries where the functions inside the code are located, so here, object code of the program is combined with object code of the selected libraries. For example, if you use a printf function, the linker will look for it in the stdio.h library, if you didn’t specified that library it will not run. Another thing it does is to verify that only one library contains the main function.The output of the linker is an executable file and when run it you will have your result.

Source: Java T point.

In conclusion, the compilation process is something that transforms a .c file written in human language into a machine language in order to make that file executable, this must be done everytime we code in C in order to have the expected outcome.