Steps to compile with GCC

Geraldine Meneses
6 min readJun 22, 2021
GCC Compiler

The word compile means to translate programming code into machine executable code. People speak a language superior to a programming language and that has its advantage and its disadvantage, that is why in the evolution of programming higher-level programming languages such as Python, Ruby and, in this case, the programming language were developed C.

What is GCC?

Its acronym is “GNU Compiler Collection” is an integrated compiler of the GNU project for C, C ++, Objective C and Fortran. Its function is to receive a source program in any of the aforementioned languages and generate a binary executable program that would be machine language.

But why compile?

Very simple, when we write a command where we order the computer to do something, it itself does not read it, it needs to see it in its language to be able to give the expected result, that is why we do the compilation, so that what we program it is converted to machine language. an example in the following image:

2. Compilation steps

What are the preprocessor, compiler, assembler, and linker?

  • The preprocessor is the one that “prepares” (modifies) the source code before being translated into machine language using the gcc -E command. The changes are made by interpreting those lines of code that begin with the symbol “#”. The use of these directives is to define the code in the language of the processor.

In the example we use the directive #include <stdio.h> where #include is the directive followed by a filename which in this case is <stdio.h>.
You must bear in mind that if the file is between the symbols “<” and “>” the preprocessor will search the internal directories of the system. This version is used to include files given by the system.

But if the file is enclosed in double quotes (“ ”) then it looks in the directory in which it is being compiled. This version of the directive is used to include files that the user has written.
The extension “.h” is used for files that are included in a program with this directive.

  • The Compiler analyzes the source code giving a detailed check for its correct execution, this process converts the commands to machine language so that the processor can read in detail its lexicon, grammar analysis, semantic analysis and if there is an error it will stop the compilation process, the The compiler is activated with the gcc -S command.

For the server to tell you what the errors were, write the -wall option that activates all the most common notices in your command. Later we will talk about the error messages that usually appear.

  • The Assembler at this point makes the language binary so that the machine can read it correctly, after going through the previous stages we know that our code is correct to be executed later here we just link it and assemble it using the gcc -c command for later be able to execute it.
  • The Link makes our source code be read by the machine in binary language and execute the requested action, to convert our file to executable we use the command gcc <file.c> -o <file>. At this last point is where we can see the result of the written code executed by the machine, it is necessary to go through the previous processes to detect and correct errors that may appear in our code and the machine can read everything correctly.

Compiler errors and warnings

The compiler is the program that, given a set of code files, translates them and generates a ready-to-run file. But for this translation to be possible, the program must meet the requirements imposed by the programming language. The type of checks that are made in this translation depends on both the programming language and the compiler itself. When something is detected in the program that is not allowed by the standards of the language, the compiler displays a message and the translation stops.

The compiler itself will not tell us exactly what errors we have in the process, but it will tell us the specific anomaly that the compiler has detected. These can be read using the gcc -wall command

but be very careful always compile with the -Wall option and do not consider the correct program until all warnings have been removed.

build process:

In this process we will compile with the blog.c file that will contain the code:

3. source code
  1. Processor. In this first stage, the compiler compiles the header file of the C program with the command:

$ gcc -E blog.c -o blog.i

This in order to do some preparations before compilation. The -E command only Solo preprocesses; it does not compile, assemble or link, be very careful with that command.

The -o command specifies the name of the output file as file, it is for the output file option.

resulting in this machine reading:

2. Compilation. In this second stage, the compiler mainly performs lexical analysis, grammar analysis, semantic analysis, etc. After checking for errors, translate the code into assembly language.

$ gcc -S blog.i -o blog.s

Each declaration in an assembly language program is described in a standard text format. A low-level machine language instruction

The -S command does not assemble or link, its only function is compilation, it can compile preprocessed files into assembly source code. This is how the machine is processing it:

3. Assembly. In this third stage the file goes through changes, becoming object code, at this level the code is already being transformed into machine language, the command would be:

$ gcc -c blog.s -o blog.o

Where the -c command compiles and assembles, but does not link the file.

At this point the blog.o file is a binary file whose byte encoding is machine language.

4. Link. In this fourth and last stage, we are going to create the executable code, giving the corresponding modification so that our code completes its stage of changes and ends up being machine language in its entirety. All this is executed with the command:

$ gcc blog.o -o blog

When completing the code in our directory we will find our file already in the execution state.

To run it just type:
$ ./blog

Inside we will not see our command since the folders were modifying their interior until they became machine language:

This is how you will see all the changes in the .c file where the green one is the already executable file.

This is the compilation process step by step with the gcc command. I hope the information is very useful, do not forget to leave a comment or any questions :)

--

--