Introduction
Hello, in this blog we are going to talk about the compilation process in c and we will explain the different steps of it.
What is compilation ?
Compilation is the process of translating source code written in high level to low level machine code. The compilation is done by a special software known as compiler.
The compiler checks source code for any syntactical or structural errors and generates object code with extension .obj (in Windows) or .o (in Linux) if source code is error free.
The c compilation process converts the source code taken as input into the object code or machine code.
The compilation process is divided into four steps:
Pre-processing, Compiling, Assembling, and Linking.
The preprocessor takes the source code as an input, and it removes all the comments from the source code.
The preprocessor takes the preprocessor directive and interprets it. For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets the directive and replace this directive with the content of the ‘stdio.h’ file.
C compilation process steps
Different steps involved in compiling a C code are:
Preprocessing:
Is the first pass of any C compilation. It processes include-files, conditional compilation instructions and macros.
Command of compilation in C for the preprocessing:
gcc -E code.c
Compiling:
Is the second pass. It takes the output of the preprocessor, and the source code, and generates assembler source code.
Command of compilation in C for the compilation:
gcc -S code.c
Assembling:
Is the third stage of compilation. It takes the assembly source code and produces an assembly listing with offsets. The assembler output is stored in an object file.
Command of compilation in C for the assembly:
gcc -c code.c
Linking:
Is the final stage of compilation. It takes one or more object files or libraries as input and combines them to produce a single (usually executable) file. In doing so, it resolves references to external symbols, assigns final addresses to procedures/functions and variables, and revises code and data to reflect new addresses (a process called relocation).
Command of compilation in C for the linking:
gcc code.c