Gcc Compiler
Language C:
C is a general purpose programming language that offers syntactic economics, flow control and simple structures and a good set of operators. It is not a very high level language and rather a small, simple language and is not specialized in any type of application. This makes it a powerful language, with an unlimited field of application and above all, you learn quickly. In a short time, a programmer can use the entire language. C is also a compiled language, as opposed to interpreted, meaning the source files written in C should be compiled in order for them to be executable.
Let's go
We need an operating system similar to Unix, also have access to the?shell and for this, we use a terminal, or terminal emulator, which is just a window that allows us to interact with the shell. Inside the terminal, we should see the shell prompt, which contains your username and machine name, followed by the PS1 environment variable that is often the "$" character and finally we need a text editor, like vi or emacs, to create a source file.
Compilation
Compilation is the conversion of code in one language into another, in a step prior to its execution. Normally when we think about compilation we talk about its most tangible version, the one that gives us an executable binary as output.
The gcc compilation process has four different steps:
1- The preprocessing
2- The compiling
3- The assembling
4- The linking
In our editor emacs we create a so called archivo.c
which contains the following:
In order for our archivo.c code to be executable, we need to enter the command “gcc archivo.c”, and the compiling process will go through all of the four steps it contains. Of course gcc has options that allow us to stop the compiling process after each step. Let’s take a look at them.
A- The?preprocessor
At this stage the directives are interpreted to the preprocessor. Among other things, the variables initialized with #define are substituted in the code for their value in all places where their name appears.
The output of this step will be stored in a file with a “.i” extension, so here it will be in archivo.i. For this we use the following command:
B- The compiler
The compilation transforms the C code into the assembly language of our machine’s processor. We can stop after this step with the “-S” option on the gcc command, and press enter.
C- The assembler
The assembly transforms the program written in assembly language to object code, a binary file in machine language executable by the processor. This will produce a file ending in “.o”.
We can stop the compilation process after this step by using the option “-c” with the gcc command, and pressing enter.
4. The linker
The C / C ++ functions included in our code, such as printf () in the example, are already compiled and assembled in existing libraries in the system. It is necessary to incorporate in some way the binary code of these functions to our executable. This is the link stage, where one or more modules in object code meet with the existing code in the libraries.
By default, after this fourth and last step, that is when you type the whole “gcc archivo.c” command without any options, the compiler will create an executable program called a.out, that we can run by typing “./a .out ”in the command line.
We can also choose to create an executable program with the name we want, by adding the “-o” option to the gcc command, placed?after?the name of the file or files we are compiling, and pressing enter:
Run by typing “./archivo-HW ” in the command line:
In one step
In a program with a single source file, the above process can be done in one step:
The archivo.o file is not created; The intermediate object code is created and destroyed without being seen by the operator, but the executable program appears there and works.
Complete example: