7.3 Libraries

A library is basically just a searchable archive of object files. If you have a number of source files that contain just functions and related data, you can turn these source files into libraries. Libraries are linked with the translated user program in a separate linking phase before the program becomes executable. Libraries can be defined to be linked statically or dynamically by a program. A library may be shared between concurrently running programs, then we have a shared library. Thus, we have three kinds of libraries – static, shared and dynamic. Each of these three types has its own advantages and disadvantages. Use of libraries to organize your total application development is generally desirable because it is good for program modularity, and code reuse – Write Once, Use Many times.

The following are the three kinds of libraries:

Static: Such a library is linked with the executable at compile-time and gets integrated with it. This results in the executable being much larger in size, but the advantage is that the final program is less dependent on versions of libraries.

Shared: Such a library is linked with the load-module at load-time, although needs to be included in the compilation command, so that compiler can generate proper address linkage information. The library does not get integrated with the executable. This results in a much smaller executable size. The down side is that the final program becomes highly dependent on versions of libraries.

Dynamic: Such a library is linked with a running program, i.e. at execution time. In C language library, there are special dlxxx() functions to do actions required for linking in and unlinking the library. The programmer has responsibility of linking the correct library. The library itself can be static or shared. The final program text size can be kept small by this method.

See Appendix C for further details about the linking and loading processes.

7.3.1 Language Library

Each High Level language generally requires the use of a run-time library of functions which are not fully implemented in the CPU hardware. Effectively, such a library extends the capability of the hardware platform and provides a more comfortable operating environment from the viewpoint of the language syntax and semantics. For example, it is quite common to have multi-dimensioned arrays as a data structure in many languages. The basic CPU does not have a direct means of providing the required array element addressability. It is the language library which provides routines to define, manipulate and provide means of element access of such arrays. Generally, due to the close co-ordination required, the language compiler developer group will prepare the language library. For C language, the library is known as libc, though there are many versions of it.

Most of the High Level languages also provide basic mathematics operations in the form of library functions, which is provided via what is known as math library. While in the language library the emphasis is on recreating the correct semantics of the language, the maths library developer is more concerned with mathematical correctness of the functions, error limits and efficiency.

7.3.2 Special Purpose Libraries

Apart from the basic language and math libraries, a particular application may require special purpose libraries. Some of the more common are – graph and tree functions, numerical methods – like matrices, statistics, special functions – graphics, sound and multimedia operations, etc.

During an application development, a programmer may find it advantageous to create his own library of the functions he finds himself to be using quite often. For example, while preparing examples for this book, the authors found it useful to have a library of binary search tree functions, called tree-base. We now summarize the steps necessary to create such libraries.

Creating Libraries: Static Library

The steps are:

  1. Create your C source files containing any functions that will be used, the library can contain multiple object files.
  2. Compile the files into object files, for example using – c option. The object files will have extension .o.
  3. Give the following commands to create the library:
    ar rc libmylib.a objf1.o objf2.o objf3.o
    This will create a static library called libmylib.a.
  4. Next give the following command:
    ranlib libmylib.a

This creates an index inside the library, which allows a fast search for the functions within the library while linking.

Static library usage: Remember to prototype your library function calls in a header file so that you do not get implicit declaration errors. When linking your program to the libraries, make sure you specify where the library can be found, for example,

gcc –o foo –L. –lmylib foo.o

The –L. option tells gcc to look in the current directory in addition to the other library directories for finding libmylib.a. You can easily integrate this into your Makefile, including even the static library setup part.

Creating Libraries: Shared Library

Creating shared or dynamic libraries is also quite simple. First, while creating the object files, you should use Position Independent Code option:

gcc –fPIC –c objf1.c
gcc –fPIC –c objf2.c
gcc –fPIC –c objf3.c 
gcc –shared –o libmylib.so objf1.o objf2.o objf3.o

The –fPIC option commands the compiler to create a Position Independent Code, i.e. create libraries using relative addresses rather than absolute addresses because these libraries can be loaded multiple times at different virtual addresses.

The –shared option specifies that an architecture-dependent shared library is being created. Note that we use gcc itself to create the library and not ar (the archiver) as in the case of the static library.

Shared library usage: To compile the application program using the shared libraries:

gcc –o foo –L. –lmylib foo.o

Note that it is exactly the same as compiling with a static library. Although it is compiled in the same way, none of the actual library code is inserted into the executable, because it is a dynamic or shared library. You can automate this process using Makefiles.

Since programs that use static libraries already have the library compiled into the program, it can run on its own.

Shared libraries dynamically access libraries at run-time, and thus the program needs to know where the shared library is stored. The actual details of arranging that are somewhat platform dependent and hence not given here.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.22.23