C is a mid-level language and needs a compiler to convert it into executable code so that the program can run on our machine.
The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU tool chain and the standard compiler for most Unix-like operating systems.
How do we compile and run a C program?
This time the gcc build is being used. To compile the program created in C, the following command is executed.
$ gcc –Wall filename.c –o filename
- The option -Wall enables all compiler’s warning messages. This option is recommended to generate better code.
The option -o is used to specify the output file name. If we do not use this option, then an output file with name a.out is generated.
What goes inside the compilation process?
Compiler converts a C program into an executable. There are four phases for a C program to become an executable:
This is the first phase through which source code is passed. This phase has the purpose of:
- Removal of Comments.
- Expansion of Macros.
- Expansion of the included files.
- Conditional compilation.
The next step is to compile filename.i and produce an; intermediate compiled output file filename.s. This file is in assembly level instructions.
In this phase the filename.s is taken as input and turned into filename.o by assembler. This file contain machine level instructions. At this phase, only existing code is converted into machine language, the function calls like printf() are not resolved.
This is the final phase in which all the linking of function calls with their definitions are done. Linker knows where all these functions are implemented. Linker does some extra work also, it adds some extra code to our program which is required when the program starts and ends.