Image taken from: https://live.mrf.io/statics/i/ps/www.muylinux.com/wp-content/uploads/2012/03/gcc-logo.png?width=1200&enable=upscale

gcc: What is it? and how to compile .c files with it on Linux.

Thanks to the GNU project for letting us code freely….

--

What is gcc?

Gcc is a compilation system that supports diverse programming languages, it is produced by the GNU Project, (Yeii free software), it is a very important tool when we want to develop software; but how it can be used to develop projects using the programming language C?

The response to the last question:

The files that are made in C need to be compiled so that a machine can understand that that language is being used, take as an example a file with an extension .c called main.c, the extension .c means that the file contains source code of the programming language c, i.e a program written in c.

gcc receives the information from this file and makes the compilation so that the computer’s processor (the brain of the machine) can interpret it, this is done in a four-stage process.

The compilation proccess:

Image taken from: https://miro.medium.com/max/518/1*wHKe6W4opLmk6pb7sxZz6w.png

Preprocessing:

In a few words this stage is a simple text replacement, a preparation of the file so that the compiler can read it cleanly, a c directive replacement is made like #define which replaces the macro in all the text, or #insert that loads the files from the libraries, in addition the comments are eliminated.

If desired, a .pp file can be created.
In gcc you can perform only the preprocessing stage with the following command:

The main.pp file will contain all the substituted information in its respective tags.

Compilation:

Here the C code is transformed into the assembly language, which is the language that can be interpreted by the machine, this creates a .s file.

To do only this part in gcc you use the following command:

A main.s file will be created containing the program in assembler language.

Assembly:

All the hardware of the computer reads information thanks to the binary language, a combination of pure 0's and 1's, in this part the assembly language is transformed to a binary file that can be interpreted and executed by the processor of the machine and by the hardware that this one controls, the code that is obtained is called object code and has the extension .o.

To do only this part in gcc you use the following command:

This creates a main.o file with binary information interpretable by the processor.

Linking:

In the c source code files functions that are in libraries are often included, like for example printf(), if these functions are used in our code it is necessary to incorporate its binary code to the object code, this phase consists in joining all the necessary binary code to create an executable that can be interpreted by the processor, the result of this stage is a binary executable file that can be interpreted directly by the processor.

There can be two types of linking:

Static: the binary code of the functions is incorporated into the binary code of the executable. This creates a larger but more autonomous executable. This is done with the command:

This creates a main executable that can be directly interpreted by the processor.

Dynamic: the binary code of the functions remains in the library, the executable loads in the memory of the computer this code and will execute it in the moment that it is needed when running the program, this creates a smaller executable but dependent on the libraries. This is done with the following command:

This creates a main executable.

The link is dynamic by effect.

To do the whole process with one command you can use the following:

Wich makes all the compilation process and creates a main executable.

Based on information on: https://iie.fing.edu.uy/~vagonbar/gcc-make/gcc.htm#:~:text=Informaci%C3%B3n%20adicional.,significa%20%22GNU%20Compiler%20Collection%22.

--

--

Mauricio Alejandro Torres Amortegui
Mauricio Alejandro Torres Amortegui

Written by Mauricio Alejandro Torres Amortegui

I am a mechatronics engineer that loves to code.

No responses yet