The GCC compiler
Luis Enrique Velásquez
Data Analyst || Power BI || SQL || Python || Pandas || ETL || EDA
The acronym GCC stands for "GNU Compiler Collection". Originally it stood for "GNU C Compiler"; GCC is still used to designate a C compilation. The GNU Compiler Collection is a set of compilers created by the GNU project. GCC is free software and is distributed by the Free Software Foundation under a general public license.
These compilers are considered standard for UNIX-derived, open source, and proprietary operating systems such as Mac OS X. GCC requires the set of applications known as binutils to perform tasks such as identifying object files or obtaining their size for copying, translating or create lists, link them, or remove unnecessary symbols.
Compilation stages
The compilation process involves four successive stages: preprocessing, compilation, assembly, and linking. To go from a human-written source program to an executable file, you need to go through these four steps in succession. The gcc command is capable of doing the whole process in one go.
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 as an example this simple test program, "circle.c" :
/**
* circle.c: Find the area of a circle.
?* Example to show compilation stages.
*/
#define PI 3.1416
main()
{
? float area, radio;
? radio = 10;
? area = PI * (radio * radio);
? printf("Circulo.\n");
? printf("%s%f\n\n", "Area de circulo radio 10: ", area);
}
Preprocessing can be requested with any of the following commands; cpp refers specifically to the preprocessor.
?
$ gcc -E circulo.c > circulo.pp
? $ cpp circulo.c > circulo.pp
Examining circle.pp
$ more circle.pp
It can be seen that the variable PI has been replaced by 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 our machine's processor.
$ gcc -S circle.c
Perform the first two stages by creating the file circle.s ; examining it with
$ more circle.s
3 - ASSEMBLY : The assembly transforms the program written in assembly language to object code, a binary file in machine language executable by the processor.
The assembler is named like as:
领英推荐
$ as -o circle.o circle.s
Creates the object code file circle.o 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 with the -c option to compile each source file separately, and then link all the object modules created. 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 whenever 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 to our executable. This is the linking stage, where one or more modules are brought together in object code with the existing code in the libraries.
The linker is named 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-tn.o