Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

3 Creating Libraries

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Creating Libraries Class Notes

Executables can be categorized into two: Static Executables: They contain fully resolved library functions that are physically linked into executable image during built. Dynamic Executable: They contain symbolic references to library functions used and these references are fully resolved either during application load time or run time.

There are mainly two types of libraries: Static Libraries (.a) Dynamically linked libraries (.so)

Steps for creating Static Libraries: 1) Implement library sources eg.: one.c

two.c

Copyright Veda Solutions (www.techveda.org)

This is only a supplementary material to classroom sessions

2) Compile sources into relocatables $gcc -c one.c -o one.o $gcc -c two.c -o two.o

3) Using Unix static library tool archive [ar] create static library $ar rcs libtest.a one.o two.o To check the number of files in libtest.a and libtest.so, we uses the commands $ar -t libtest.a $nm -s libtest.a It will list all object files also all the functions inside the object file. In our case it will show one.o and two.o.

Thus created a static library libtest.a and in order to check this library lets write a C program test.c which calls the two functions test() and test1() in one.c and two.c. $vim test.c #include<stdio.h> int main() { test(); test1(); } Then we need to compile test.c with the static library libtest.a . Generally linker checks the default path only, but our library is in our working directory therefore while compiling we have to specify the path of our library in order to avoid undefined reference error.

$gcc test.c -o teststat ./libtest.a

Copyright Veda Solutions (www.techveda.org)

This is only a supplementary material to classroom sessions

This will create an executable teststat which is statically linked

Steps for creating Dynamic libraries: Generally relocatables are of two types Position dependent: Relocatables which cannot be shared. Position independent: Relocatables which can be shared.

First create two .c files one.c and two.c as above. Compile the sources into position independent relocatables

$gcc -c -fpic one.c -o one.o $gcc -c -fpic two.c -o two.o -fpic is a flag which is used to tell the linker as it is position independent.

Using dynamic linker creating shared library $gcc -shared -o libtest.so one.o two.o Then compile the above test.c using dynamic library libtest.so. $gcc test.c -o testdyn ./libtest.so

In both the cases output will be same. In order to understand the major differences between static and dynamic linking, we will analyze the executables using objdump tool.

$ objdump D teststat

[static executable]
This is only a supplementary material to classroom sessions

Copyright Veda Solutions (www.techveda.org)

If we analyze the main function in objdump file we could see that test function base address is given and it is called directly. $ objdump D testdyn [dynamic executable]

Here in dynamic executables main function, it is calling test@plt , (plt= procedure linkage table). Plt table is generated by linker and contains information about dynamic linking.
Copyright Veda Solutions (www.techveda.org) This is only a supplementary material to classroom sessions

Above figure shows the plt table. In that table we could find a function pointer, which derefers to our particular function test.

USE CASES Static executables occupies more disk space but it has zero initialization time. Dynamic executables consumes less disk space but it consumes n amount of cpu cycles for initialization. Static builds are preferred if executables are being built for a specific use and will be used in a resource constrained environment where initialization delays are not tolerable. Dynamic builds are preferred for easier customization, maintenance and extensions for an application.

Copyright Veda Solutions (www.techveda.org)

This is only a supplementary material to classroom sessions

You might also like