13
Compilation process Lecture-1 Compilation process

Compilation process Lecture-1 Compilation process

Embed Size (px)

Citation preview

Page 1: Compilation process Lecture-1 Compilation process

Compilation process

Lecture-1

Compilation process

Page 2: Compilation process Lecture-1 Compilation process

C Compilation process

Preprocessor

Compiler

Assember

Linker

Components of Compiler

Assembly code

Object code Libraries

Source code( .c file)

Executable file

Page 3: Compilation process Lecture-1 Compilation process

Preprocessor

Preprocessor

/* this is demo */#include<stdio.h>void main(){ printf (“hello”);}

code of stdio.h filevoid main(){ printf (“hello”);}Preprocessor remove comments and include

header files in source code, replace macro name with code.

Page 4: Compilation process Lecture-1 Compilation process

Compiler

Compiler

code of stdio.h filevoid main(){ printf (“hello”);}

push ebpmov ebp, espand esp, -16sub esp, 16mov eax, OFFSET FLAT:.LC0mov DWORD PTR [esp], eaxcall printfleaveret

Compiler generate assebmly code.

Page 5: Compilation process Lecture-1 Compilation process

Assembler

Assembler

Assebler convert assemble code into object code.

push ebpmov ebp, espand esp, -16sub esp, 16mov eax, OFFSET FLAT:.LC0mov DWORD PTR [esp], eaxcall printfleaveret

001010011001010011010111101010010101010010101011010010101010100101010100110011111110010001010010101001010111110111111111111101000000001110010010

Page 6: Compilation process Lecture-1 Compilation process

Linker

Linker

0010100110010100110101111010100101010100101010110

0010100110010100110101111010100101010100101010110

0010100110010100110101111010100101010100101010110

Libraries

Page 7: Compilation process Lecture-1 Compilation process

Linker: Static linking

Linker

001010011001010011010111110

Libraries

001010011001010011010111110

Call function one()

#include<stdio.h>void main(){ one(); }

01010100000101001010

one()

01010100000101001010

Code of function one included in final executable file.

Page 8: Compilation process Lecture-1 Compilation process

Linker: Static Linking

Linker

Object file1

Object file2

Libraries

Code of file 1 calling 200 funtions.Code of file 2 calling 100 functions.

Size ?

Page 9: Compilation process Lecture-1 Compilation process

Linker:Static Linking exe file generated.1

User take exe file.2

Some changes made in one function.

3

Libraries010100

Again compilation needed so that exe file include updated one function.

4

Page 10: Compilation process Lecture-1 Compilation process

Linker: Dynamic linking

Linker

001010011001010011010111110

Libraries

001010011001010011010111110Address of one function in memory

Call function one()

#include<stdio.h>void main(){ one(); }

01010100000101001010

one()

Memory address, where one function is loaded , included in final executable file.

Memory

0x2EA126One functionLibrary

Page 11: Compilation process Lecture-1 Compilation process

Linker: Dynamic Linking Benefits over static linking

Size No need to compile

Page 12: Compilation process Lecture-1 Compilation process

Library Static Library

Windows .lib (Library) Linux .a (Archive)

Dynamic Library Windows .dll (Dynamic link library) Linux .so (Shared object)

Page 13: Compilation process Lecture-1 Compilation process

Commands Linux (gcc)

Preprocessor step ( gcc -E <filename>) Compiler step( gcc -S <filename>) Generate Object code(gcc -O <filename>) Generate executable (gcc <object code>)