These are the basic compilation stages in normal C. Any C program will passed through all these stages to complete it’s execution. Each and every stage have its own set of operations.
Let us discuss each of them in detail.
Write and Edit Source Code:
Source code normal c code we wrote in the high-level language in other words program what we are writing on IDE or any notepad etc. Normally we used to save this code by .c extension.
Pre-processor Step:
Pre-processed Directives are always started with Hash Symbol (#).
C program is are passed through the preprocessor before going to Compiler.
These preprocessors directives are used to add Header files to the program.
The main thing is pre-processor Directives are used to include files to our programs.
Commonly used Preprocessor Directives are #include and #define.
#include is used for Including Files may be Header files or Normal Files.
#define is used to Define Symbolic Constants and Macros.
Generally we are using stdio.h header file using #include pre-processor Directive for including standard input and output files to our program so that we can perform printing values by using printf function and reading values by using scanf function.
In pre-processing stage a set of operations are done by pre-processor, Those are
Removing Comments
Macro Substitution
File Inclusion (Including Header files)
Conditional Compilation
After this stage, we will get pure C code without any other things like comments.
Translator:
Translator translates the High-level language into Assembly level language. After this stage, we will get Assembly level code something like ADD, SUM, SUB, SJMP,..etc mnemonics.
Assembler:
Assembler is also on the type of translator, but it is used to translate assembly level language into Machine language (machine language is computer understandable language contains zeros and ones only ). After this stage, we will get pure binary code. This code also called as object code.
Linker:
Linker used to link the called function with calling function. Normal C program contains many library functions (predefined functions) and user-defined functions. Library functions present in library for example we are using printf daily, but we are not defined printf we are just calling printf and printing data, functions like printf are needed in our program so those printf predefined code is present in library that code is added to our object code in this stage. So linker links called function with calling function.
After this stage, our Executable is ready and stored in your computer secondary storage space.
Loader:
Loader brings executable file into the primary memory from the secondary memory
Any application needs to bring into primary memory in order execute, but our all executable files are stored in secondary memory loader is used to bring that files from secondary memory to primary memory. Typically Hard disk to RAM
If you’re using Unix or Linux the normal gcc prog.c is equal to first five stages and we will get a.out by compiling then by running that a.out we will get our output.
Now we will discuss one example with each and every stage output files..
Example:
This is our source code
$ vi Prog.c
#include<stdio.h>
void main()
{
printf(“Hello World..n”); // This is Comment
}
To get pre-processed code use
$gcc –E prog.c –o prog.i
Here prog.c is our source code and prog.i is pre-processed file. That means comments are removed and header files are included.
Just observe below code…
This is our source code
This is Pre-processed code
Observe above code. Header files are included and comment is are removed.
To get translated code use
$gcc –S prog.i –o prog.s
Here prog.i is pre-processed code and prog.s is translated code
Prog.s (Translated code)
This is Assembly level code. Contains all mnemonics like add, sub, push, SJMP,..
To get Assembled code use
$ gcc –c prog.s –o prog.o
Here prog.s is translated code and prog.o is Object code.
can you give command for linking only.
For example how to use ld command