GCC

Felipe Balanta
3 min readJun 17, 2021
In the world of programming and computers, we must know that they do not understand any of the programming languages with which we have been working, since these machines have a numbering system. the numbers are represented using only two digits zero (0) and one (1). It is one of the systems used in computers, because they work internally with two voltage levels, so its natural numbering system is the binary system.

¿What is GCC?

It is a compiler for GNU considered standard for Operating Systems derived from UNIX, Open Source or also proprietary Operating Systems, such as Mac OS X. The acronym GCC stands for GNU Compiler Collection (Collection of GNU compilers). Before this acronym for GNU C Compiler (GNU C Compiler). As its name suggests, it is a collection of compilers and supports various languages: C, C ++, Objective C, Chill, Fortran, Ada, and Java.

Compilation Types

The compilation process involves four successive stages: preprocessing, compilation, assembly, and linking. To go from a human-written source program to an executable file, these four steps must be performed in succession. The gcc and g ++ commands are capable of doing the whole process at once.

1. Preprocessed.
At this stage the directives to the preprocessor are interpreted. Among other things, variables initialized with #define are substituted in code for their value in all places where their name appears.
We will use this simple test program, circle.c, as an example:

/ * Circle.c: calculates the area of a circle.
Example to show compilation stages.
* /
#define PI 3.1416
main ()
{
float area, radius;
radius = 10;
area = PI * (radius * radius);
printf (“Circle. \ n”);
printf (“% s% f \ n \ n”, “Circle area radius 10:”, area);
}

Preprocessing can be requested with any of the following commands; cpp refers specifically to the preprocessor.

$ gcc -E circle.c> circle.pp
$ cpp circle.c> circle.pp

Examining circle.pp

$ more circle.pp

it can be seen that the variable PI has been substituted for its value, 3.1416, as it had been set in the #define statement.

2. Compilation.
The compilation transforms the C code into the assembly language of the processor of our machine.

$ gcc -S circle.c

perform the first two stages by creating the file circulo.s; examining it with

$ more circle.s

you can see the program in assembly language.

3. Assembled.
Assembly transforms the assembly language program into object code, a machine language binary file executable by the processor.
The assembler is named like this:

$ as -o circle.o circle.s

creates the object code file circle.or from the assembly language file circle.s. It is not common to do just the assembly; the usual thing is to carry out all the previous stages until obtaining the object code like this:

$ gcc -c circle.c

where the file circle.o is created from circle.c. The file type can be verified using the command

$ file circle.o

circle.o: ELF 32-bit LSB relocatable, Intel 80386, version 1, not stripped

In large programs, where many source files are written in C code, it is very common to use gcc or g ++ with the -c option to compile each source file separately, and then link all created object modules. These operations are automated by placing them in a file called makefile, interpretable by the make command, which takes care of making the minimum necessary updates every time any portion of code is modified in any of the source files.

4. Linked
The C / C ++ functions included in our code, such as printf () in the example, are already compiled and assembled in existing libraries on the system. It is necessary to incorporate somehow the binary code of these functions into our executable. This is the link stage, where one or more modules are brought together in object code with the existing code in the libraries.
The linker is called ld. The command to bind

$ ld -o circle circle.o -lc

ld: warning: cannot find entry symbol _start; defaulting to 08048184
gives this error for lack of references. You need to write something like

$ ld -o circle /usr/lib/gcc-lib/i386-linux/2.95.2/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o circle / usr / lib / crt1. or /usr/lib/crti.o /usr/lib/gcc-lib/i386-linux/2.95.2/crtbegin.o -L / usr / lib / gcc-lib / i386-linux / 2.95.2 circle.o -lgcc -lc -lgcc /usr/lib/gcc-lib/i386-linux/2.95.2/crtend.o /usr/lib/crtn.o

to get an executable.

Direct use of the ld linker is very rare. Instead the object codes are supplied directly to gcc:

$ gcc -o circle circle.o

create the executable circle, which invoked by its name

$ ./circle

Circle.
Circle area radius 10: 314.160004
gives the displayed result.

--

--