Library are precoded, precompiled files that are ready for use.
Programmers in addition to their own source code they will use the others code by including the file in their source code. It is possible to do like that if only one or two programs have to be referenced. In order to include multiple files it becomes more complex.
So, UNIX allows the property of creating the library objects. There are two kinds of library objects.
1. Static library (.a extension)
2. Dynamic Library (.so extension)
Archive library is a static library. These files end withextension .a , then archive library are called along with the program so they become the part of the program execution. The archive library are smaller in size when compared to the shared library.
You can create your own library files by using the following,
STEP1: Create your own object file by compiling yourprogram.
gcc –c filel.c file2.c
The command line option –c is given to produce the object file. The above command produces the two object file file1.o and file2.o
STEP2: ARCHIVE THE FILES TO BE USED FOR LIBRARY OBJECT
ar -q libmyarchive.a file1.o file2.o
The above command creates the library libmyarchive.a with two object files file1.o and file2.o. The command line option q is used for quickappend. It is used to append the member to the archive.
To view the members of the obect use the
ar -t libmyarchive.a
This will display the members of the libmyarchive such asfile1.o and file2.o
Other useful command line options:
-d : deletes the member of archive
-x : extracts the member
-V : modifies the version number of the archive
After the successful creation of libray you can include in asource file by the following
gcc newfile.c libmyarchive.a
gcc newfile.c –lmyarchive
The second option will work if you include the library underthe /opt/usr/lib and remember that the file name you have created should prefixwith a lib.
Using ranlib option:
The ranlib option is used for creating the index for the archive file you have been created. The ranlib stands for randomize library.
If you have 100 object file in archive then loader needs to look at all the 1000 files to load the library object. In order to speed up the process ranlib program in GNU creates the index for the archive. Its like the table of content in a book.
Shared library is a dynamic library. These files end with extension .so, then shared library is not called along with the program, they are called whenever they are needed. The archive library are larger in size when compared to the archive library.
STEP1 : Compile the program as PIC:
So are you wondering what is the PIC. PIC is the process independent code. It means it will not depend on the memory where the code was stored. All shared library objects should be a PIC.
g++ -c –fpic test1.cpp
Yes fpic is the command line option that is used for creating the process independent code and PIE stands for process independent executable.
STEP2: Creating the shared Library using –shared
Now you have to modify the .o file into .so file. I mean object file in to the shared object file. To do so,
g++ -shared –o libtest_shared.so test.o
Here libtest_shared.so is created from file test.o
STEP3: Linking and exporting TO LD_LIBRARY_PATH(Loader Library path)
Now you have to inform the compiler about the shared library about where he is present to access it. For that unix uses the LD_LIBRARY_PATH environmental variable.
After informing the compiler you should export the LD_LIBRARY_PATH to your current shell.
Try to learn about rpath too. It is the command line option that is used to give the path of the shared library at run time.
library naming convention:
Library are stored under the /usr/lib/. Every library object is prefixed with a lib.
Eg: libm.a (maths libary), libpthread.a (thread library)
They are called while compilation by the following format.
g++ file1.cpp –lm –lpthread