Programmer can not write similar function bodies again and again which are used very frequent in programming. To reduce this overhead, library is created. Library contains all those functions called library function, which is used often in programming i.e pritf(), scanf(), read(), write() and many more. All these function bodies are stored in object files in library. Libraries are compiled separately and linked to your program on type of linking basis. two types of linking used to add objects to program and they are compile time linking (Static linking) and run time linking(Dynamic linking), explained below.
Based on linking with code library divided into two types 1) Static Library or Linking. 2) Dynamic Library or Linking.
Static Linking (Static library)
It is simplest form of library where all objects files are collectively kept in ready-to-use form. Before using this library, programmers need to include header which contains function declaration and use -l option(explained later) so that compiler and linker can link all function type to library function and create a single executable file. all the linking happens at last stage of compilation called linking stage. Programmers can create their own static library and method is very simple :).
Here we are creating two library function to demonstrate static linking. These two files contains function for adding and subtraction.
step-1 : Create add.c
File 2 sub.c
It is not always mandatory to create multiple files for all separates function. It is up to programmers based on requirement and type of application they can crate as many as files or all can contain in a single file.
Step-2 Compile both add.c and sub.c
Step-3 create one .h file locally where u can define your library functions ex: my_lib.h
Step-4 Create archives of name libmylib.a using command 'ar' including both add.o and sub.o object files. In terms of linker to identify any kind of library, library name should must start with "lib". Other wise programmer may face "/usr/bin/ld: cannot find -lmylib" kind of error.
Step-5 Create main.c file where all library function created above can be used
main.c
Add local header file into main to avoid "implicit declaration of function" kind of warning. And declaration of prototypes is good programming practice.
Step-6 Now compile main file and link static library to program.
-l option tells compiler to link lib<libraryname> to executable file so that all library functions used in program can be linked with their bodies.
Step -7 Run the executable file "run"
Dynamic Linking (Shared/Dynamic library)
Unlike static linking, dynamic linking happens at run time so that once program started running all library function will load and unload as soon as usage of function is over. Dynamic libraries are stores same place as static library, but filename for shared libraries are different. mostly denoted as libmylib.so.
Creating a shared library
Like static it is also collection of object files with the difference of, it will not combine with program at compilation time.
Will make use of same programs and files which were used to create static library.
Step-1 :Compile add.c and sub.c using -fPIC or fpic option it is needed to create shared library.
Step-2 : Create shared library (.so) file by using command
libmyshared.so is created as shared library.
Step-3 : Now use this shared library in program
It is not yet done before running executable file we need to copy this .so file to /usr/lib/
Step -4 Run the executable file "run"
There are many advantages of using Shared library over Static library...
References:
1. Beginning Linux Programming, 4th Edition
Neil Matthew, Richard Stones
2.Program-Library-HOWTO
Based on linking with code library divided into two types 1) Static Library or Linking. 2) Dynamic Library or Linking.
Static Linking (Static library)
It is simplest form of library where all objects files are collectively kept in ready-to-use form. Before using this library, programmers need to include header which contains function declaration and use -l option(explained later) so that compiler and linker can link all function type to library function and create a single executable file. all the linking happens at last stage of compilation called linking stage. Programmers can create their own static library and method is very simple :).
Creating a Static library
Here we are creating two library function to demonstrate static linking. These two files contains function for adding and subtraction.
step-1 : Create add.c
#include <stdio.h>
int my_library_add(int arg1, int arg2)
{
printf("Add function\n");
return (arg1 + arg2);
}
File 2 sub.c
#include <stdio.h>
int my_library_sub(int arg1, int arg2)
{
printf("Sub function\n");
return (arg1-arg2);
}
It is not always mandatory to create multiple files for all separates function. It is up to programmers based on requirement and type of application they can crate as many as files or all can contain in a single file.
Step-2 Compile both add.c and sub.c
$ gcc -c add.c sub.c
$ ls
add.c add.o sub.c sub.o
Step-3 create one .h file locally where u can define your library functions ex: my_lib.h
#include <stdio.h>
int my_library_add(int, int);
int my_library_sub(int, int);
Step-4 Create archives of name libmylib.a using command 'ar' including both add.o and sub.o object files. In terms of linker to identify any kind of library, library name should must start with "lib". Other wise programmer may face "/usr/bin/ld: cannot find -lmylib" kind of error.
$ar crv libmylib.a add.o sub.o
a - add.o
a - sub.o
$ ls
$ ls
add.c add.o libmylib.a main.c my_lib.h sub.c sub.o
Step-5 Create main.c file where all library function created above can be used
main.c
#include <stdio.h>
#include <stdlib.h>
#include "my_lib.h"
int main(int argc, char *argv[])
{
int arg1, arg2, sum, sub;
if ( argc != 3)
{
printf("Invalid args<Enter arg1:integer arg2:integer>");
exit(0);
}
arg1 = atoi(argv[1]);
arg2 = atoi(argv[2]);
sum = my_library_add(arg1,arg2);
printf("Sum : %d\n",sum);
sub = my_library_sub(arg1,arg2);
printf("Sub : %d\n",sub);
exit(0);
}
Add local header file into main to avoid "implicit declaration of function" kind of warning. And declaration of prototypes is good programming practice.
Step-6 Now compile main file and link static library to program.
gcc -Wall -I. -o run main.c -lmylib
-l option tells compiler to link lib<libraryname> to executable file so that all library functions used in program can be linked with their bodies.
Step -7 Run the executable file "run"
$ ./run 5 8
Add function
Sum : 13
Sub function
Sub : -3
Dynamic Linking (Shared/Dynamic library)
Unlike static linking, dynamic linking happens at run time so that once program started running all library function will load and unload as soon as usage of function is over. Dynamic libraries are stores same place as static library, but filename for shared libraries are different. mostly denoted as libmylib.so.
Creating a shared library
Like static it is also collection of object files with the difference of, it will not combine with program at compilation time.
Will make use of same programs and files which were used to create static library.
Step-1 :Compile add.c and sub.c using -fPIC or fpic option it is needed to create shared library.
$ gcc -fPIC -g -c -Wall add.c
$ gcc -fPIC -g -c -Wall sub.c
$ ls
add.c add.o main.c sub.c sub.o
Step-2 : Create shared library (.so) file by using command
$ gcc -shared -o libmyshared.so add.o sub.o
$ ls
add.c add.o libmyshared.so main.c sub.c sub.o
libmyshared.so is created as shared library.
Step-3 : Now use this shared library in program
# gcc -Wall -o run main.c libmyshared.so
It is not yet done before running executable file we need to copy this .so file to /usr/lib/
# cp *.so /usr/lib/
or can be installed using # ldd run
linux-vdso.so.1 => (0x00007fff9c3c2000)
libmyshared.so => (0x00007fff9c3c3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f81fa14e000)
/lib64/ld-linux-x86-64.so.2 (0x00007f81fa539000)
Step -4 Run the executable file "run"
$ ./run 5 8
Add function
Sum : 13
Sub function
Sub : -3
- When we use static library it links before creation of executable file(linking stage) hence as a result large size of .exe file. whereas shared library gets loaded on run time so does not affect the size of .exe file.
- when multiple applications are running and all are using same library, all ended up with redundant copy of same function in memory. As many as times the function is called it will create a separate image of that function in memory and this is not at all good for memory constrain applications. Dynamic library is shared among all the application so all are using same image of function multiple times, no extra image is created for library function.
- In static linking all library functions are loaded before running the code and unloaded after execution is done. Dynamically linked library (shared libraries) are loaded on ad-hoc basis on run time, only loaded into memory when it is needed and unloaded immediately after use.
- Static library has to be linked explicitly using -l<libname> option but shared library will link automatically at run time.
- Shared library can be updated separately for the application rely on it.
References:
1. Beginning Linux Programming, 4th Edition
Neil Matthew, Richard Stones
2.Program-Library-HOWTO
No comments:
Post a Comment