An Introduction To GCC Step-By-Step

Kevin Ramos
5 min readFeb 4, 2021

--

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:

  • Readability: Programs are easy to read.
  • Maintainability: Programs are easy to maintain.
  • Portability: Programs are easy to port across different computer platforms.

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

Let’s write it down:

  • Pre-processor (Pre-processing)
  • Compiler (Compiling)
  • Assembler (Assembling)
  • Linker (Linking)

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:

  • It will get rid of the comments that you made in the source code.
  • It will include those header files (libraries) that you called in the source code.
  • The pre-processor will replace the macros with their definitions throughout the program.

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:

  • It will link all the sources files together (object files) and can even combine one program with another one. Just to get it simple, it will take, for example, the “secondary.o” and combine it with the “main.o” to make one program.
  • Another thing is that, before all this, the linker will only receive reference of a function. But the linker will resolve those reference and link the object file with the libraries.

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!

--

--

No responses yet