Gcc is a set of compilers produced by the GNU project. Gcc originally stands for GNU Compiler Collection because it only compiled the c language, however it was later extended to compile C ++, Fortran, Ada among others.
Gcc receives a source program in any of the aforementioned languages and generates a binary executable program in the machine language. In order for gcc to convert programs into executables, it first performs four important steps.
Preprocessing, Compilation, Assembly and linking
The preprocess begins to discard the parts of the program that are unnecessary in this case discard the lines of the comments marked by the symbols / ** /. Then include the libraries marked with the #include symbol that goes in the header of the program. And finally it ends with the substitution of the macros that we have defined in the program. #define
Macros are widely used in C and C ++. These are basically an alias that we can include in our code which, when compiling, will be replaced by what we have defined. … The output of this program is: “The value of the macro is: 100”.
To preprocess a program you can use the following command:
$ gcc -E [Filename.c]
After the process it goes to compilation. The compilation basically generates embed code to the language of the machine. The program will go to the machine code. This almost decipherable for human reading due to its complex structure.
What’s Machine Code?
The machine language or machine code is the system of codes that can be directly interpreted by a microprogrammable circuit, such as the microprocessor of a computer or the microcontroller of the same.
To convert a program from a c file into assembly code use the following command:
$ gcc -S [Filename.c]
The output will look like the following:
After compilation we proceed to assemble our program. The assembly transforms the program written in base 2 binary code language or object code, a binary file in machine language executable by the processor.
What’s Object Code?
Object code generally refers to the output, a compiled file, which is produced when the Source Code is compiled with a C compiler. The object code file contains a sequence of machine-readable instructions that is processed by the CPU in a computer.
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
You can use this command to get the object code:
gcc -c [Filename.c]
what this command does is link programs without linking them and converts it to object code
The output will look like the following
The last step is done by linking. this step supports the program already converted into object code and supports the precompiled libraries. Then the modules are compressed in an only file called executable. By default, the executable throws us an a.out file but if you want a name other than the default, we can change it using the flat -o.
Use the following command to rename the executable:
gcc [Filename.c] -o [name you want]
To execute the a.out file we do it as follows