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
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