The 4 Basic Steps of the GNU Compiler
C is a compiled language, which means any code written in C needs to be compiled into a binary executable file before it can run. Compiling a single file program like the main.c program above goes through 4 steps of processing before it can output a single executable file, which in this example will simply print the text “Hello World!” to the screen.
The basic command used to compile a C file in the terminal is gcc filename.c:
$ gcc main.c
STEP 1: THE PREPROCESSOR
During preprocessing, GCC looks at each line in the C file, and performs text substitution on any #include and #define statements amongst other things, inserting new code into the program to include any supporting functions and statements needed, and removing comments which are unneeded for the final program to run. As an example, in main.c, the very first statement #include <stdio.h> is substituted with all of the code that makes up the standard input/output library for C, which allows the use of C’s printf function in the program.
STEP 2: THE COMPILER
The compiler step converts the preprocessed file from step 1 into Assembly code, a set of machine instructions that allows the compiler to further convert the program into machine readable code during the next step.
STEP 3: THE ASSEMBLER
During assembly, the compiler converts the set of assembly statements/instructions it created into object code — machine readable code in the form of binary digits 0 and 1 — which is how the machine is able to understand and run the program.
STEP 4: THE LINKER
The final linker step packages all the object code created and adds any extra dependencies needed to allow the program to run on the machine, and creates an executable file.