There are many things that happen to your source code when you try to compile it with
gcc. I will go over the steps of compilation that get your source code to turn in executable code using
What is gcc?
The GNU Compiler Collection (GCC) is a compiler system used to translate source code (code that is written by you, the programmer) into executable code for you by running the command
gcc. When referring to the name of GCC (GNU Compiler Collection), you use capitals and when you refer to the command you use in the terminal, you use lowercase
gcc. This system can recognize multiple languages of code and will adjust accordingly. At the beginning it only handled the C language, but it has now extended to C++, Objective-C, Objective-C++, Fortran, Java, Ada, Go, and even more. It is a free software that played a role in encouraging the growth of other free software.
There are four steps that your code will go through once the
gcc command is executed. These steps are, in order: Preprocessor, Compiler, Assembler, and Linker.
The Preprocessor section of the compilaton process takes the initial source code (your code) and starts out by taking out all the comments and including the header files (like
At first, lets create a source code file named source_code.c.:
#include <stdio.h>printf("Hello World\n");
* main - Entry point
* Return: Always 0 (Success)
Now run the command:
$ gcc -E source_code.c -o test_file
This will stop the compilation process and write out the results of the Preprocessing step into the file test_file. To look into the file simply run
cat test_file. If you don’t stop the compilation process, it will move on to the next step, which is the Compiler step.
This Compiler section of the compilation process takes the newly edited code (with no comments and the included header files) and translates this code into a new type of code called the assembly code. To take a look at this new file, run the command:
$ gcc -S source_code.c -o test_file
cat the file again and you can see that the code has changed drastically. After the Compiler has done it’s job, the code now gets passed to the Assembler.
This code is now used by the assembler to build object code, i.e. code containing only 0’s and 1’s and passes it to the Linker. It is also possible to stop the processes at this point and look at the new file by running the command:
$ gcc -c source_code.c -o test_file
To look at the file
cat test_file again. Once this new file is created, it gets passed on to the Linker.
The Linker takes the system’s libraries and combines them with the object code to make executable code. Once you reach executable code, you can run it like an executable file. So to go through the process, without stopping it at certain points, you would run the command:
$ gcc source_code.c -o test_file
Now you have executable code inside the file test_file. To run this file simply type:
And you can see that our code now runs and prints the message “Hello World”. This is how the source code goes through the compilation process to become executable code.