What exactly does “gcc” do?
What does gcc
do? Well, first of all, what is gcc
? gcc
is a command line, C compiler found on GNU-based operating systems. Not familiar with the command line? Check out my blog post on Under the Hood of the Command “ls *.c”.
The command gcc
stands for “GNU C Compiler”. Let’s go over this piece by piece, starting with GNU. GNU stands for “GNU’s not Unix!”, which is a bit of a joke acronym. GNU is a UNIX-like operating system and set of free software.
Next is “C”. C is a programming language developed by Dennis Ritchie. C is a static-typed procedural programming language. C was written to replace the assembly code that was previously used to write very low-level programs such as operating systems or processes for supercomputers.
The remainder of this post will be about the “compiler” aspect of the the gcc acronym. What does it mean to compile C code? Well, we can break it down into 4 main steps: Preprocessing, Compiling, Assembly, and Linking.
Preprocessing
During the preprocessing step, 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.
First, at the head of each C file are any header files to include additional capability to the C program. The preprocessor will take the source code for that header and actually insert it into the program in order for the program to actually take advantage of those functions.
Second, any macros get expanded, meaning that any macros defined at the top of the file will have their actual value replace any instances of the macro within the file.
Lastly, the preprocessor 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.
I hope this post brings you a better understanding of exactly what happens when compiling C source code with gcc
.