The GCC Compiler
First things first, a compiler is a program that translates a somewhat high level programming language to machine code (binary) that can be understood by computers. So we can get an executable file from our source code.
GCC stands for GNU’s Compiler Collection, which is part of the GNU project founded by Richard Stallman in the 80s. These people are responsible for the now very popular Linux operating systems; a Unix-based system created to be free and open source. The acronym GNU means “GNU is Not Unix!”… someone thought that was funny.
Gcc works as a command in bash, so: gcc filename.c
would compile a C program. Let’s add some common options to see what they do:
gcc -Wall filename.c -o anotherfile
This compiles the source code in ‘filename.c’ to machine code and stores it in an executable file ‘anotherfile’. The output file for the machine code is specified using the -o
option. This option is usually given as the last argument on the command line. If it is omitted, the output is written to a default file called 'a.out'.
Note that if a file with the same name as the executable file already exists in the current directory it will be overwritten.
The option -Wall
turns on all the most commonly-used compiler warnings---it is recommended that you always use this option! There are many other warning options which you can learn more about at: https://gcc.gnu.org/
Now, what does the gcc compiler really do?…
Preprocessing
During the pre-processing step, the source code from header files is included, the value of each macro gets replaced with its fill-in name, and all comments are removed from the code.
At the head of each C file are the header files to include additional capability to the C program. The pre-processor will take the source code from that header and actually insert it into the program in order for the program to actually make use of those functions.
Lastly, the pre-processor will remove any comments in the source code as they are not used by the computer.
Compiling
At the compilation step, a preprocessed file is turned into machine-specific code. The instructions at this step get converted from C instructions to low-level processor and memory commands.
Assembly
At this stage, the low-level compiler code is turned into binary which can be directly interpreted by the processor.
Linking
The linking stage brings in external commands from referenced files into the binary file so the computer has the full understanding of how to run the commands. Also at this stage, the output becomes executable meaning that the user can run the code.
Sources: