What happens when you type gcc to compile a .c file?

Stages of the compilation process

To carry out the compilation process, four successive stages are covered: preprocessing, compilation, assembly and linking. With this, it is possible to go from a source program written by a human to an executable file. The gcc command can perform the entire process at once.

1. Preprocessing

In this first stage the preprocessor directives are interpreted. The lines in the code that begin with the “#” character are preprocessor directives. Tasks such as text substitution, stripping of Comments, file Inclusion are performed in this stage.

2. Compilation

The compilation transforms the C code into the assembly language specific to the processor of our machine.

3. Assembly

The assembly transforms the program written in assembly language to object code, a binary file in machine language executable by the processor.

4. Linking

The C / C ++ functions included in our code, such as printf () in the example, are already compiled and assembled in existing libraries on the system. It is necessary to somehow incorporate the binary code of these functions into our executable. This is what the link stage does, where one or more modules are brought together in object code with the existing code in the libraries.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store