What Happens When You Type GCC main.c?
First let’s understand what GCC is. GCC stands for GNU Compiler Collection. GCC is a set of compliers used to build programs and make them executable. This involves a four step process.
The initial stage of compilation is the preprocessor. Here is where your code goes, and lines starting with # are interpreted as preprocessor commands, such as your header file. If you want to stop the process at the preprocessor, you can do that one of two ways.
gcc -E helloworld.c
or you can call on the preprocessor directly
cpp helloworld.c
The second stage of the compilation process, is compilation. At this point the preprocessed code is translated into the assembly instructions. If you would like to stop the compilation process here, you can do so by using -S
gcc -S helloworld.c
This will create the file helloworld.s containing the generated assembly instruction.
The third stage is the assembly process. In this stage an assembler is used to translate the assembled instructions into object code. This creates actual code to be run by the processor. If you want to stop it at this point, you can use -c
gcc -c helloworld.c
This will create the file helloworld.o containing the object code of the program. At this point the code is in binary.
The final stage is linking. At this point the linker fills in any blanks and rearranges the code as needed to make the file run. The result of this stage is an executable file.
So when you enter gcc main.c, all four steps are completed, ending with an executable file.