GCC — GNU Compiler Colection
What is compilation?
The compilation process converts the source code taken as input into the object code or machine code. An Example of a compiler is GCC (GNU Compiler Collection) a set of compilers created by the GNU project and I’s a open source software.
How to compile a C program?
We can compile a “.c” file using the below command:
$ gcc myFilename.c –o myExeFilename
After compilation a executable is generated and we can run the generated executable using the below command:
$ ./myExeFilename
The compilation process
The compilation process can be divided into four steps: Pre-processing, Compiling, Assembling, and Linking.
Pre-processor
It’s the first program invoked by the compiler and processes directives such as #include, #define and #if, take a “.c” file as an input and will produce a “.i” file as an ouput.
Pre-processor make few operations such as:
- Header file inclusion
- Macro substitution
- Comment removal
- Conditional compilation
We can trigger the pre-processor using the below command:
$ gcc -E myFile.c > myFile.i
Compiler
Compiler take the output of the pre-processor , an “.i” file, to get Assembly instructions as an onput and will produce a “.s” file.
In compiler we have two different stages:
Analysis phase
- Linear/Lexical analysis
- Syntax analysis
- Semantic analysis
Synthesis phase
- Intermediate code generation
- Code generation
- Code optimization
Use the below comand to get the compiler output (the assembly instructions):
$ gcc -S myfile.i
Assembler
Assembly take the output of the compiler, a “.s” file, to get binary code as an output, and will produce an “.o” file.
Use the below comand to produce the compiler output (the object file):
$ gcc -c myFile.s
Linker
Linker combine the object code of library files with the object code of our program, the output of the linker is the executable file.
Use the below comand to get the executable object file:
$ gcc -O myfile.o
The .i, .s and .o files are delete after the compilation process. We can use the below comand to keep them:
$ gcc -save-temps myFile.c