I wanted to know in depth meaning and working of compiler, linker and loader. With reference to any language preferably c++.
相关问题
- How do shader compilers work?
- C++ Builder - Difference between Lib and Res
- Direct2D Only Partially Linking in C++ Builder
- What are the recommended GNU linker options to spe
- What is the right order of linker flags in gcc?
相关文章
- MPI and D: Linker Options
- How to force Delphi compiler to display all hints
- Xcode - How to change application/project name : w
- Weakly link static library via -weak_library
- What other neat tricks does the SpecialNameAttribu
- What is the purpose of “-Wa,option” in GCC? [close
- Why does the C++ compiler give errors after lines
- Can a compiler inline a virtual function if I use
C preprocessor :-
C preprocessing is the first step in the compilation. It handles:
#define
statements.#include
statements.The purpose of the unit is to convert the C source file into Pure C code file.
C compilation :
There are Six steps in the unit :
1) Lexical Analyzer:
It combines characters in the source file, to form a "TOKEN". A token is a set of characters that does not have 'space', 'tab' and 'new line'. Therefore this unit of compilation is also called "TOKENIZER". It also removes the comments, generates symbol table and relocation table entries.
2) Syntactic Analyzer:
This unit check for the syntax in the code. For ex:
The above code will generate the parse error because the equation is not balanced. This unit checks this internally by generating the parser tree as follows:
Therefore this unit is also called PARSER.
3) Semantic Analyzer:
This unit checks the meaning in the statements. For ex:
The above code generates the error "Assignment of incompatible type".
4) Pre-Optimization:
This unit is independent of the CPU, i.e., there are two types of optimization
This unit optimizes the code in following forms:
I) Dead code elimination:
For ex:
Here, the compiler knows the value of 'a' at compile time, therefore it also knows that the if condition is always true. Hence it eliminates the else part in the code.
II) Sub code elimination:
For ex:
can be optimized as follows:
III) Loop optimization:
For ex:
In the above code, if 'a' is local and not used in the loop, then it can be optimized as follows:
5) Code generation:
Here, the compiler generates the assembly code so that the more frequently used variables are stored in the registers.
6) Post-Optimization:
Here the optimization is CPU dependent. Suppose if there are more than one jumps in the code then they are converted to one as:
The control jumps to the directly.
Then the last phase is Linking (which creates executable or library). When the executable is run, the libraries it requires are Loaded.
Linker & Interpreter are mutually exclusive Interpreter getting code line by line and execute line by line.
A compiler is a software program that compiles program source code files into an executable program. It is included as part of the integrated development environment IDE with most programming software packages. The compiler takes source code files that are written in a high-level language, such as C, BASIC, or Java, and compiles the code into a low-level language, such as machine code or assembly code. This code is created for a specific processor type, such as and Intel Pentium or PowerPC. The program can then be recognized by the processor and run from the operating system.
Loader is An operating system utility that copies programs from a storage device to main memory, where they can be executed. In addition to copying a program into main memory, the loader can also replace virtual addresses with physical addresses. Most loaders are transparent, i.e., you cannot directly execute them, but the operating system uses them when necessary.
Linker Is a program that adjusts two or more machine-language program segments so that they may be simultaneously loaded and executed as a unit Also called link editor and binder, a linker is a program that combines object modules to form an executable program. Many programming languages allow you to write different pieces of code, called modules, separately. This simplifies the programming task because you can break a large program into small, more manageable pieces. Eventually, though, you need to put all the modules together. This is the job of the linker.
ASCII representation:
*
explained with respect to, linux/unix based systems, though it's a basic concept for all other computing systems.
*
Linkers and Loaders from LinuxJournal explains this concept with clarity. It also explains how the classic name a.out came. (assembler output)
A quick summary,
c program --> [compiler] --> objectFile --> [linker] --> executable file (say, a.out)
we got the executable, now give this file to your friend or to your customer who is in need of this software :)
when they run this software, say by typing it in command line ./a.out
execute in command line ./a.out --> [Loader] --> [execve] --> program is loaded in memory
Once the program is loaded into the memory, control is transferred to this program by making the PC (program counter) pointing to the first instruction of
a.out
Compiler:
It will read source file which may be of type .c or .cpp etc and translates that to .o file called as object file.
Linker:
It combines the several .o files which may be generated for multiple source files into an executable file (ELF format in GCC). There are two type of linking:
Loader:
A program which loads the executable file to the primary memory of the machine.
For an in-detail study about the these three stages of program execution in Linux, please read this.