Gcc Compiler

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.

No hay texto alternativo para esta imagen

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

No hay texto alternativo para esta imagen

which contains the following:

No hay texto alternativo para esta imagen

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:

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen
No hay texto alternativo para esta imagen

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:

No hay texto alternativo para esta imagen

Run by typing “./archivo-HW ” in the command line:

No hay texto alternativo para esta imagen

In one step

In a program with a single source file, the above process can be done in one step:

No hay texto alternativo para esta imagen

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:

No hay texto alternativo para esta imagen









要查看或添加评论,请登录

Rodrigo Mato的更多文章

  • Static Libraries in C

    Static Libraries in C

    What Are Header Files and What Are Libraries Header files, such as stdio.h, contain function declarations and macro…

社区洞察

其他会员也浏览了