SlideShare una empresa de Scribd logo
1 de 20
Libraries
16/05/15 2
Library:
• It may contain a group of functions that are used in a particular
context.
• Functions which can be shared by more than one applications
are broken out of the application’s source code, compiled and
bundled into a library.
Types :
• Static
• Dynamic/Shared
16/05/15 3
Static Library(archives) (extension- .a):
• Every executable program have a copy of library code.
Dynamically linked shared object libraries(extension- .so):
• Dynamically loaded/unloaded and linking during execution
using the dynamic linking loader system functions.
• The shared objects are not included into the executable
component but are tied to the execution.
16/05/15 4
Static Library
•Typically named with the prefix “lib”.
For example:
Math library - libmath.a (static)
Shared Library
●Name used by linker - libmath.so
●Fully qualified name or soname - libpthread.so.1
●Real name - libpthread.so.1.1
Version
Number
Minor Number
Library Naming Convention
16/05/15 5
Location in File System
- According to File Heirarchy Standards
• /lib - loaded at startup
• /usr/lib - used by system internally
• /usr/local/lib - non standard distribution
Usage of ldconfig
Linker
Symbolic
link
Fully Qualified
“soname”
16/05/15 6
• ar is an archive tool used to combine objects to create an
archive file with .a extension, i.e, library.
1) Create 2 sample C programs
2) Compile the programs and get the object code
3) Create the C program static library using ar utility
4) Write C program to use the library libarith.a
Steps to Create Static Library
16/05/15 7
1) Create two sample C programs:
int addition(int a, int b) int multiplication( int a, int b)
{ {
int result; int result;
result=a+b; result=a*b;
} }
2) Compile and get object codes:
$ gcc –c addition.c
$ gcc –c multiplication.c
Current working directory contains both c and object files.
$ ls
addition.c multiplication.c addition.o multiplication.o
16/05/15 8
3) Create the C program Static Library using ar utility:
$ ar cr libarith.a addition.o multiplication.o
4) Write c program to use the library libarith.a:
Create header.h Create example.c:
#include<stdio.h> #include “header.h”
Int addition(int a, int b); int main()
Int multiplication(int a, intb); {
int result;
result=addition(1,2);
printf(“addition result is :
%dn”
, result);
result=multiplication(3,2);
printf(“multiplication result is :
%dn”, result);
16/05/15 9
• Compile example.c
$ gcc –Wall example.c libarith.a –o example
Result:
$ ./example
addition result is :3
multiplication result is : 6
16/05/15 10
To Command Result
List Object Files $ ar t libarith.a addition.o
multiplication.o
Extract object files from
archive
$ ar x libarith.a
$ ls *.o
Addition.o
Multiplication.o
Add object file into existing
archive file
$ ar r libarith.a subtraction.o Addition.o
Multiplication.o
Subtraction.o
Delete the specific archive
member
$ ar d libarith.a addition.o Multiplication.o
Subtraction.o
16/05/15 11
1. Create code that has to be added to Shared Library
2. Create a header file
3. Create a shared library
4. Write a program which uses shared library
5. Link the code with shared library
6. Set the environment variable
7. Run the program
Steps to Create Shared Library
16/05/15 12
Step 1. shared.c:
#include “shared.h”
Unsigned int add(unsigned int a, unsigned int b)
{
Printf(“n Inside add()n”);
Return(a+b);
}
Step 2. Shared.h:
#include<stdio.h>
extern unsigned int add(unsigned int a, unsigned int b);
16/05/15 13
Step 3.
gcc –c –Wall – werror –fPIC shared.c
gcc –shared –o libshared.so shared.o
Step 4.
#include<stdio.h>
#include”shared.h”
int main(void)
{
unsigned int a=1;
unsigned int b=2;
unsigned int result =0;
result=add(a,b);
printf(“n the result is [%u]n”, result);
return 0;
}
16/05/15 14
Step 5.
gcc –L/home/Desktop/practice/ -wall main.c –o main –lshared
Step 6.
export LD_LIBRARY_PATH=/home/desktop/practice:$
LD_LIBRARY_PATH
Step 7.
$ ./main
Inside add()
The result is [3]
16/05/15 15
16/05/15 16
Static Library Shared Library
Increased memory for each program Contains a record of
1. Name of the symbol
2. Which library is it from
Time Consuming Time Efficient
Re-linking difficulties Run time linking using Fully Qualified
soname
Run time Execution is relatively fast Run time Execution is relatively slow
Objects of unresolved symbols get
copied to the address space
Shared library is mapped to address space
of program
Extension - .a Extensions - .so(.x.x)
Difference b/w Static & Shared Library
16/05/15 17
How Static Library Works?
File a.o b.o Libx.a Liby.a
Object a.o b.o X1.o X2.o Y1.o Y2.o
Definitions a1,a2,A3 b1,B2,b3 x11,X12 X21,x22 y11,Y12 y21,
Y22
Undefined
refernce
b2,x12 a3,y22 x23,y12 y11 y21
16/05/15 18
int e=7;
int main() {
int r = a();
exit(0);
}
main.c
add.c
extern int e;
int *ep=&e;
int x=15;
int y;
int a() {
return *ep+x+y;
}
How Shared Library Works?
16/05/15 19
Merging .o files into an executable
int e = 7
headers
system code
main()
a()
int *ep = &e
more system code
system data
int x = 15
.text
.data
uninitialized data .bss
.symtab
.debug
system code
system data
.text
.data & .bss
main()main.o
int e = 7
.text
.data
add.o
int *ep = &e
a()
int x = 15
int y
.text
.data
.bss
a()
Relocatable object files
0
Executable object files
16/05/15 20
“by Sriee Gowthem Raaj”

Más contenido relacionado

La actualidad más candente

Computer architecture kai hwang
Computer architecture   kai hwangComputer architecture   kai hwang
Computer architecture kai hwang
Sumedha
 
GFS - Google File System
GFS - Google File SystemGFS - Google File System
GFS - Google File System
tutchiio
 
30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt
raj732723
 

La actualidad más candente (20)

Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
Computer architecture kai hwang
Computer architecture   kai hwangComputer architecture   kai hwang
Computer architecture kai hwang
 
File system security
File system securityFile system security
File system security
 
IO Management
IO ManagementIO Management
IO Management
 
File System in Operating System
File System in Operating SystemFile System in Operating System
File System in Operating System
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
GFS - Google File System
GFS - Google File SystemGFS - Google File System
GFS - Google File System
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMS
 
Introduction to Parallel Computing
Introduction to Parallel ComputingIntroduction to Parallel Computing
Introduction to Parallel Computing
 
30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt
 
Computer architecture multi processor
Computer architecture multi processorComputer architecture multi processor
Computer architecture multi processor
 
File Management in Operating Systems
File Management in Operating SystemsFile Management in Operating Systems
File Management in Operating Systems
 
Multiprocessor architecture
Multiprocessor architectureMultiprocessor architecture
Multiprocessor architecture
 
MemoryManagementStrategies.ppt
MemoryManagementStrategies.pptMemoryManagementStrategies.ppt
MemoryManagementStrategies.ppt
 
Network security
Network securityNetwork security
Network security
 
Beneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBeneath the Linux Interrupt handling
Beneath the Linux Interrupt handling
 
Operating system 03 handling of interrupts
Operating system 03 handling of interruptsOperating system 03 handling of interrupts
Operating system 03 handling of interrupts
 
Page Replacement Algorithms
Page Replacement AlgorithmsPage Replacement Algorithms
Page Replacement Algorithms
 
Unix ch03-03(2)
Unix ch03-03(2)Unix ch03-03(2)
Unix ch03-03(2)
 
static libraries and dynamic libraries
static libraries and dynamic librariesstatic libraries and dynamic libraries
static libraries and dynamic libraries
 

Similar a Libraries

Understanding how C program works
Understanding how C program worksUnderstanding how C program works
Understanding how C program works
MindBridgeTech
 

Similar a Libraries (20)

Linkers
LinkersLinkers
Linkers
 
From gcc to the autotools
From gcc to the autotoolsFrom gcc to the autotools
From gcc to the autotools
 
Autotools
AutotoolsAutotools
Autotools
 
Autotools pratical training
Autotools pratical trainingAutotools pratical training
Autotools pratical training
 
C Under Linux
C Under LinuxC Under Linux
C Under Linux
 
Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008
 
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptx
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Build your own discovery index of scholary e-resources
Build your own discovery index of scholary e-resourcesBuild your own discovery index of scholary e-resources
Build your own discovery index of scholary e-resources
 
Build Systems with autoconf, automake and libtool [updated]
Build Systems with autoconf, automake and libtool [updated]Build Systems with autoconf, automake and libtool [updated]
Build Systems with autoconf, automake and libtool [updated]
 
Lecture 8_Libraries.pptx
Lecture 8_Libraries.pptxLecture 8_Libraries.pptx
Lecture 8_Libraries.pptx
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?
 
Advanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAdvanced debugging  techniques in different environments
Advanced debugging  techniques in different environments
 
Understanding how C program works
Understanding how C program worksUnderstanding how C program works
Understanding how C program works
 
Composer namespacing
Composer namespacingComposer namespacing
Composer namespacing
 
Process (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSProcess (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oS
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
Libraries
LibrariesLibraries
Libraries
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
 

Más de University of Texas at Dallas

Más de University of Texas at Dallas (15)

Interaction overview & Timing diagram
Interaction overview & Timing diagramInteraction overview & Timing diagram
Interaction overview & Timing diagram
 
Communication Diagram
Communication DiagramCommunication Diagram
Communication Diagram
 
Sequence Diagram
Sequence DiagramSequence Diagram
Sequence Diagram
 
State Diagram
State DiagramState Diagram
State Diagram
 
Package Diagram
Package DiagramPackage Diagram
Package Diagram
 
Deployment Diagram
Deployment DiagramDeployment Diagram
Deployment Diagram
 
Component Diagram
Component DiagramComponent Diagram
Component Diagram
 
Composite Structure Diagram
Composite Structure DiagramComposite Structure Diagram
Composite Structure Diagram
 
Object diagram
Object diagramObject diagram
Object diagram
 
Class diagram
Class diagramClass diagram
Class diagram
 
Use Case UML Diagram
Use Case UML DiagramUse Case UML Diagram
Use Case UML Diagram
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
Yocto project
Yocto projectYocto project
Yocto project
 
Subversion Reference
Subversion ReferenceSubversion Reference
Subversion Reference
 
Subversion
SubversionSubversion
Subversion
 

Último

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Último (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 

Libraries

  • 2. 16/05/15 2 Library: • It may contain a group of functions that are used in a particular context. • Functions which can be shared by more than one applications are broken out of the application’s source code, compiled and bundled into a library. Types : • Static • Dynamic/Shared
  • 3. 16/05/15 3 Static Library(archives) (extension- .a): • Every executable program have a copy of library code. Dynamically linked shared object libraries(extension- .so): • Dynamically loaded/unloaded and linking during execution using the dynamic linking loader system functions. • The shared objects are not included into the executable component but are tied to the execution.
  • 4. 16/05/15 4 Static Library •Typically named with the prefix “lib”. For example: Math library - libmath.a (static) Shared Library ●Name used by linker - libmath.so ●Fully qualified name or soname - libpthread.so.1 ●Real name - libpthread.so.1.1 Version Number Minor Number Library Naming Convention
  • 5. 16/05/15 5 Location in File System - According to File Heirarchy Standards • /lib - loaded at startup • /usr/lib - used by system internally • /usr/local/lib - non standard distribution Usage of ldconfig Linker Symbolic link Fully Qualified “soname”
  • 6. 16/05/15 6 • ar is an archive tool used to combine objects to create an archive file with .a extension, i.e, library. 1) Create 2 sample C programs 2) Compile the programs and get the object code 3) Create the C program static library using ar utility 4) Write C program to use the library libarith.a Steps to Create Static Library
  • 7. 16/05/15 7 1) Create two sample C programs: int addition(int a, int b) int multiplication( int a, int b) { { int result; int result; result=a+b; result=a*b; } } 2) Compile and get object codes: $ gcc –c addition.c $ gcc –c multiplication.c Current working directory contains both c and object files. $ ls addition.c multiplication.c addition.o multiplication.o
  • 8. 16/05/15 8 3) Create the C program Static Library using ar utility: $ ar cr libarith.a addition.o multiplication.o 4) Write c program to use the library libarith.a: Create header.h Create example.c: #include<stdio.h> #include “header.h” Int addition(int a, int b); int main() Int multiplication(int a, intb); { int result; result=addition(1,2); printf(“addition result is : %dn” , result); result=multiplication(3,2); printf(“multiplication result is : %dn”, result);
  • 9. 16/05/15 9 • Compile example.c $ gcc –Wall example.c libarith.a –o example Result: $ ./example addition result is :3 multiplication result is : 6
  • 10. 16/05/15 10 To Command Result List Object Files $ ar t libarith.a addition.o multiplication.o Extract object files from archive $ ar x libarith.a $ ls *.o Addition.o Multiplication.o Add object file into existing archive file $ ar r libarith.a subtraction.o Addition.o Multiplication.o Subtraction.o Delete the specific archive member $ ar d libarith.a addition.o Multiplication.o Subtraction.o
  • 11. 16/05/15 11 1. Create code that has to be added to Shared Library 2. Create a header file 3. Create a shared library 4. Write a program which uses shared library 5. Link the code with shared library 6. Set the environment variable 7. Run the program Steps to Create Shared Library
  • 12. 16/05/15 12 Step 1. shared.c: #include “shared.h” Unsigned int add(unsigned int a, unsigned int b) { Printf(“n Inside add()n”); Return(a+b); } Step 2. Shared.h: #include<stdio.h> extern unsigned int add(unsigned int a, unsigned int b);
  • 13. 16/05/15 13 Step 3. gcc –c –Wall – werror –fPIC shared.c gcc –shared –o libshared.so shared.o Step 4. #include<stdio.h> #include”shared.h” int main(void) { unsigned int a=1; unsigned int b=2; unsigned int result =0; result=add(a,b); printf(“n the result is [%u]n”, result); return 0; }
  • 14. 16/05/15 14 Step 5. gcc –L/home/Desktop/practice/ -wall main.c –o main –lshared Step 6. export LD_LIBRARY_PATH=/home/desktop/practice:$ LD_LIBRARY_PATH Step 7. $ ./main Inside add() The result is [3]
  • 16. 16/05/15 16 Static Library Shared Library Increased memory for each program Contains a record of 1. Name of the symbol 2. Which library is it from Time Consuming Time Efficient Re-linking difficulties Run time linking using Fully Qualified soname Run time Execution is relatively fast Run time Execution is relatively slow Objects of unresolved symbols get copied to the address space Shared library is mapped to address space of program Extension - .a Extensions - .so(.x.x) Difference b/w Static & Shared Library
  • 17. 16/05/15 17 How Static Library Works? File a.o b.o Libx.a Liby.a Object a.o b.o X1.o X2.o Y1.o Y2.o Definitions a1,a2,A3 b1,B2,b3 x11,X12 X21,x22 y11,Y12 y21, Y22 Undefined refernce b2,x12 a3,y22 x23,y12 y11 y21
  • 18. 16/05/15 18 int e=7; int main() { int r = a(); exit(0); } main.c add.c extern int e; int *ep=&e; int x=15; int y; int a() { return *ep+x+y; } How Shared Library Works?
  • 19. 16/05/15 19 Merging .o files into an executable int e = 7 headers system code main() a() int *ep = &e more system code system data int x = 15 .text .data uninitialized data .bss .symtab .debug system code system data .text .data & .bss main()main.o int e = 7 .text .data add.o int *ep = &e a() int x = 15 int y .text .data .bss a() Relocatable object files 0 Executable object files
  • 20. 16/05/15 20 “by Sriee Gowthem Raaj”