Let’s talk a little bit about GCC

Oscar Morales
3 min readJun 10, 2020

--

First at all, what is GCC?

If we ask this to Wikipedia, we get the following definition:

The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. When it was first released in 1987, GCC 1.0 was named the GNU C Compiler since it only handled the C programming language. It was extended to compile C++ in December of that year. Front ends were later developed for Objective-C, Objective-C++, Fortran, Java, Ada, and Go, among others.

If we ask for the manual to the bash or any shell that we use, we get the following definition:

GCC — GNU project C and C++ compiler (<man gcc>)

Then,we can see that there is a similar word in both definitions: compiler

What is a compiler?

A compiler is a program that translate code language into an executable program. Behind, there are four components that carry out this translate process: preprocessor, compiler, assembler and linker.

  • Preprocessor: prepare the code language, removing comments, including the header files used in the source and replacing macro name with code
  • Compiler: translate the code prepared by the preprocessor into assembly code (Instructions defined by English words called mnemonics)
  • Assembler: translate the assembly code into machine language (Binary code): machine language can only be processed by microprocessors (CPU’s), I mean, CPU’s only understand binary code
  • Linker: If we are working with different sources of code, the linker combining these in only one executable program. Also, if we are using different libraries, the linker linked these libraries with the executable program

Let’s see these steps with an example

We are going to create a C program that print “This is an example”. You can use your favorite text editor, I chose emacs for this example.

<emacs main.c>: Creates a C code source file called main

We can stop the compilation process with the following options

-E: Preprocess only; do not compile, assemble or link
-S: Compile only; do not assemble or link
-c: Compile and assemble, but do not link

Let’s see what happen!

<gcc -E main.c > p>: stops the compilation process just after the preprocessor and store the output in the p file

As we can see, preprocessor prepare the code language, removing comments and including the header files used, in this case <stdio.h>

<gcc -S main.c>: stops the compilation process just after the assembler

<gcc -S main.c> creates by default a file with the same name of the source code file but with .s extension.

<gcc -c main.c>: stops the compilation process just after the linker

<gcc -S main.c> creates by default a file with the same name of the source code file but with .o extension.

<gcc main.c -o example>: Creates an executable program called example that prints “This is an example”

It is interesting to know how a compiler works to understand the relationship between a “human readable” code language and a machine language, and how this becomes a program that it is created with a particular aim.

--

--