Introduction
The compilation is the process of transforming source code into object code. It is accomplished with the help of the compiler. The compiler checks the source code for syntactical or structural errors, and if it is error-free, it produces the object code.
Recommended Topic, Sum of Digits in C, C Static Function
Compilation in C
C is a compiled programming language. Its source code is written in the form of a text file using any editor of a programmer's preference, and then it must be compiled into machine code.
The C compilation process converts the input source code into object code or machine code. Pre-processing, Compiling, Assembling, and Linking are the four phases in the compilation process.
C source files are typically named with .c extension. We compile C source files with the command "gcc" (GCC is an abbreviation for GNU Compiler Collection, a compiler system developed by the GNU Project).
Let us now examine what happens when we run gcc main.c and learn about the four key steps of the compilation process.
Before we begin, let's make a .c file and run the program to observe what occurs.
So we made a file called main.c and wrote a C program within it that prints "Hello world!!." We built an executable file a.out after compiling it. We put it to the test, and it did indeed print "Hello world!!". Let's go a little further and wonder what happened and how we ended up with a.out.
Also read, Bit stuffing program in c
PRE-PROCESSING
This is the very first stage that a source code goes through. The following tasks are completed during this stage:
- Comment Removal
- Macros Expansion
- Expansion of the files contained
- Conditional compilation
To further understand preprocessing, we will build the above 'main.c' program with the flag -E, which will print the preprocessed output to standard output. See the example below.
The source file in the above result is filled with a lot of information, but our code is maintained at the end.
To proceed and make things clearer, we will use the following command:
$ gcc -save-temps main.c
The -save-temps flag instructs the compiler to save the gcc compiler's temporary intermediate files in the current directory. So we'll obtain the files main.i, main.s, and main.o, as well as the executable a.out.
Must Read Passing Arrays to Function in C
COMPILING
When the compiler has completed the preprocessing stage, the subsequent phase is to accept the main.i as input, compile it and generate an intermediate compiled result. This stage's output file is called 'main.s'. The assembly level instructions are contained in the output of main.s.
The picture demonstrates that it is written in assembly language, which an assembler can understand.
Also check out - Phases of Compiler and Short int in C Programming
ASSEMBLY
At this point, the main.s file is used as input, and the intermediate file main.o is created. This file, also known as the object file, is created by the assembler, which recognizes and translates a '.s' file containing assembly instructions into a '.o' object file containing machine-level instructions. At this point, only existing code is transformed into machine language; function calls such as printf() remain unresolved.
Because the result of this stage is a machine-level file, it is entirely incomprehensible and will appear as
By looking at this output, we can explain
Hello World !!= the content to be shown, which we entered in main.c, and the Ubuntu version from which the GCC compiler is running.
LINKING
This is the final stage in which all function calls are linked to their definitions. The linker is aware of where all of these functions are implemented. Linker also performs some more work, adding some extra code to our application that is necessary when the program starts and stops. For example, there is code that is necessary for environment setup, such as giving command line parameters. Using $size filename.o and $size filename, you can simply validate this task. We can see how the output file grows from an object file to an executable file with these instructions. This is due to the additional code that the linker includes with our software.
You can also read about C dynamic array.
Must Read: Difference Between Compiler and Assembler.