An Introduction To GCC Step-By-Step

To understand GCC and all the process of the compilation behind the scene it’s important to first talk about C language (just the basic, don’t worry), the gcc and what happen after we compile a C file.

Programming language C

C is a programming language that was first developed in 1972 by Dennis Ritchie at AT&T Bell Labs. It was called C because there was already a B language and, fun fact, B led the development of C.

C is a high-level language but since some of the most popular high-level languages created right now are better in many ways than C, some may call C the lowest high-level programming language. High-leve languages got common advantages:

Since high-level languages benefit directly from human languages, like English, we need some compilers or interpreter to translate instructions written in a high-level programming language into a machine language that a computer can understand. This is where gcc comes from since it’s a compiler.

GNU Compiler Collection (GCC)

GCC supports several languages like C, C++, and FORTRAN. But we are going to talk about the gcc compiler for C. Now, to understand how to use it, we need to understand the process behind the compilation.

The process of compilation

A compilation is a process that translate the source code (the code we write) into an object code (machine language) by the compiler. In this case, gcc.

The Process of compilation in C
The Process of compilation in C
The Process of Compilation in C

Let’s write it down:

To start the process we need the compiler gcc and the source code. Let’s talk a little bit about the source code that we are going to use.

The Source Code

Like we said earlier, the source code, is the code that we write in our language using functions and variables.

So this is our source code. We got header files (library), comments, and finally our program. This source code will be called “main.c”. It’s a default name for example purposes. To clarify, the “.c” is the extension for C language files. And we are going to convert that extension throughout the process of the compilation. For this, we will need to use the command gcc and write the name of our source code. It should go like this:

After that, the file will go through the steps of the process.

The Four Steps of the Compilation Process

STEP 1: Pre-processor

This is the first step of the compilation process. In here, the compiler will take somethings out and pass another ones. We can define this in 3 things:

You can pre-process a C file using the option “-E” after the gcc like it show below:

STEP 2: Compiling

In this step, the compiler will take the pre-processed file and generate an IR code (Intermediate Representation). Is the language of an abstract machine designed to aid in the analysis of computer program. This can be done with the “-S” option with the gcc command like it show below:

And this is the content of the “main.s” file:

STEP 3: ASSEMBLING

Now, in this step it will take the the “main.s” and change it into an object code (machine language). This file will end with “.o” extension. Using the option -c with the gcc command like it shows below:

The content of the object file will look like this:

STEP 4: LINKING

This is the final step, where this will combine the object file with the libraries and give us the executable file that we need to launch our program. In here, will happen two things:

This can be done using the command “gcc” without any option like it shows below:

The problem is, for those who like to put their own name on the files, that it will create for default a “a.out.” executable file. If we want to change this we can use this option “-o” after our source code like it show below:

Now, to test our program we just need to test it out using “./” and the name after that slash like the image below:

And the output will depend on your program. So, all this, and more, happens behind the scene. There’s a lot more information in the web to search, so like we say: “Google it, and then, google it again”. Happy programming!