The image shown below can brief you out the different stages and file extensions during the compilation of a C program.
Preprocessing:
Stages of Compilation in C |
A preprocessor produces pure C code. The actions performed
for this are – removal of comments, file inclusion, macro replacement, pragma
processing and conditional compilation.
The input to a pre-processor is a .c file and the output is a
.i file.
Preprocessing |
Preprocessing using gcc is done using the following command
on the terminal.
gcc – E test.c -o test.i
Translation:
Translator converts the pure C code into assembly level code
i.e., from high-level code to low-level code. This is the level of compilation
where all the syntaxes are checked. The .i file generated after pre-processing
is given as input to the translator, which generates .s file.
Translation |
Translation using gcc is done using the following command on
the terminal.
gcc – S test.i -o test.s
Assembling:
An assembler produces target-based code from the pure C
code. This is the main stage where the original C code written by the programmer
becomes hardware specific, by generating respective micro-instructions for a
given hardware.
The input to an assembler is a .i file generated after
translation and the output file comes with .o extension.
Assemblilng |
Assembling using gcc is done using the following command on
the terminal.
gcc – c test.i -o test.o
Linking:
When you are working with multiple source files, the
respective objects files are generated and are compiled together to produce a
combined application. The linker links all the dependencies of a project and
generates single application that can be finally executed. The input to the
linker is .o file and the output file is an executable file with no extension.
Linking |
Linking using gcc is done using the following command on the
terminal.
gcc – c test1.c test2.c test3.c -o test
This command “links” the source code files test1.c, test2.c
and test3.c altogether and generates single executable file test, which is the
targeted application.
If you have single file, then the following command is to be used.
If you have single file, then the following command is to be used.
gcc test.o -o test
where 'test' is the executable file generated.Then, the executable file can be executed as follows:
./test
If you want to skip all the four stages of compilation, you can directly get the executable file from the source file itself, as follows:
gcc test.c
With this command, the operating system produces the default executable file a.out, which can be executed as follows: ./a.out
However, there are two types of linking – Static and Dynamic linking, which will be discussed in later sessions.
0 comments:
Post a Comment