The Steps of Compilation — Using the GCC Compiler

Caroline
2 min readFeb 4, 2021

As you may already know, computers don’t speak English… or Spanish. At their core, computers speak binary code. They can only process information in 1s and 0s. So how does a code, written with a language like C or Python, end up being understood by the computer? That is when the compiler comes into play.

A compiler is a program that translates code written in a programming language, into another language. They are a language processor. In this post I will specifically discuss the steps the GCC (GNU Compiler Collection) compiler takes when compiling source code.

Before starting to compile your code, if you are using C, make sure you give your file a .c extension. Then use the “gcc” command to compile your file. You can find different options for the “gcc” command here: https://man7.org/linux/man-pages/man1/gcc.1.html.

There are four steps the GCC compiler takes: preprocessing, compiling, assembly, and linking.

Step number 1: Preprocessing.

During this period, comments are removed from the code, header files and defined macros are expended. Any code from the header files/external functions used will be substituted in.

Step number 2: Compiling.

During this stage, the output from the preprocessor is converted to assembly language. Assembly language is still semi readable by humans. An intermediate language between source code and binary.

Step number 3: Assembly.

In this part the assembler will convert the assembly code into machine readable language, binary code. If any external functions are being used, their address will remain, later be addressed by the linker.

Step number 4: Linking.

This is the last step of the compilation process. The linker merges all the code and if any external libraries are being used, the linker will merge the external code in. The linker also does some additional tasks as well to allow our program to run.

These are the four major steps the compiler takes!

You can use the code below to run the “gcc” compiler. The -o option will allow you to specify a different name for your output file, instead of the standard “a.out” name.

gcc -o <desired_output_filename> <source filename>

For any modifications you may want to make, read https://man7.org/linux/man-pages/man1/gcc.1.html.

--

--

Caroline
0 Followers

Holberton School Programming Student