Difference between static(.a) and shared library(.so) ?
Archived objects(.a) i.e. static libraries are compiled into the program itself. When multiple programs/application uses the static library then multiple times same pre-compiled functions in that library gets included in to the program as well as in the main memory so size of application increases.
But shared library over comes this issue.
Shared Objects (.so) i.e. shared libraries are compiled separately and only reference/linked given such that only required functions are loaded into the memory at run time. so small size of the application as it does not contains the function code.
steps to create static library(archives) .a
step1:writing the function in library as library is nothing but set of pre compiled functions
we use funcInt(int i) and funcChar(char *charp)
/*funcInt.c:first function to be precompiled for our library*/
void funcInt(int i)
printf("\nint version library function called.\n");
/*funcChar:Second function to be archived in our library*/
void funcChar(char *charp)
printf("\nchar version library function called.\n");
step2:creating the objects files
compile the above functions with -c option which will create only object file and avoid creation of executable program.
(without -c above functions will not compile also as no main function)
gcc -c funcInt.c funcChar.c
it will show the created object files :
funcChar.o and funcInt.o
step3:creating the static library say ourlibrary by precompiling these functions and archiving it.
according to naminf=g convention it will be prefixed with lib and post fixed with .a
ar -crv libourlibrary.a funcInt.o funcChar.o
Note that lib prefixing is must.
step4: writing header file say ourheader.h having declarion of these function in ourlibrary
any application/program want to use this library must include this header file.
/*header file to be included for uing the created library*/
void funcInt(int i);
void funcChar(char *charp);
step5: using the created library in main funcions say funcMain.c
create the calling function in main and include the header file containing the library function declaration
#include "ourheader.h"/*header file to use static libraries created above*/
//calling the library function created
funcChar("calling the library function funcChar.");
gcc -o programexecutable funcMain.c funcChar.c
gcc -o programexecutable funcMain.o libourlibrary.a
gcc -o programexecutable funcMain.o -lourlibrary
ms@ubuntu:~/libraryCreation$ ar -crv libourlib.a funcChar.o funcInt.o
a - funcMain.o
a - funcChar.o
a - funcInt.o
ms@ubuntu:~/libraryCreation$ gcc -o m funcMain.o libourlib.a
char version library function called.
1. Static libraries are compiled into the program itself.
2. Program Size increases.
3. Every program has its own static library.
1. Shared libraries are compiled separately and referenced by the program.
2. Program size is smaller but shared libraries are required at the run time.
3. Shared library has only one copy and referenced by different programs.
Process to create static library
1. gcc -c mylibrary.c -o mylibrary.o
2. ar rcs libmylibrary.a mylibrary.o
Process to create shared library
1. gcc -c -fPIC mylibrary.c -o mylibrary.o
2. gcc -shared -Wl,soname,libmylibrary.so.1 -o libmylibrary.so.1.0.1 mylibrary.o
Tar the source (if needed) using this command
tar-zcf libmylibrary.tar.gz libmylibrary/
How to use static library :
Compile :gcc -static test.c -L. -lmylibrary -o statexec
Run : ./statexec
How to use dynamic/shared library :
Compile :gcc test.c -o dynamicexec -L. –lmylibrary
Run : ./dynamicexec
You May Like:
C Linux Code for multi threaded socket client/Server file sharing ftp