You are on page 1of 8

Static Libraries

What they are, why they're useful.


An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files (called members of the archive). A static library is an archive whose members are object files. A library makes it possible for a program to use common routines without the administrative overhead of maintaining their source code, or the processing overhead of compiling them each time the program is compiled.

How they're named, version numbers, symbolic links.


Static libraries have names like libfoo.a, with no version numbers. There are normally no symbolic links associated with a static library.

Debugging symbols.
?

Where they're installed.


Static libraries are installed in /usr/lib or /usr/local/lib, not in /lib. They are used only during compilation. They are not critical to the operation of the system, so need not be on the root filesystem.

How ld finds them.


ld looks for the C library libc automatically. Otherwise, the option -lfoo will cause it to search its path-list for the library libfoo.a. ld looks for libraries in standard directories, plus those specified by -Ldir options on its command line.

What nm can tell you.


nm can list the symbols defined in a library. For example, "$ nm --print-file-name /usr/lib/*.a|grep cbrt" will show that the symbols containing the string "cbrt" are in libm.a (the math library).

Example commands to build a static library.


ar rs libfoo.a foo1.o foo2.o

Example commands to compile and link using a local static library.


gcc -I. -o jvct jvct.c libjvc.a

Example commands to install a static library.


install -m 644 libjvc.a /usr/lib

Example commands to compile and link using an installed static library.


gcc --static -I. -o jvct jvct.c -ljvc

Shared Libraries.
What they are, why they're useful.
Each program using routines from a static library has a copy of them in the executable file. This wastes disk space and (if more than one such program is in use at the same time) RAM as well. Also, when a static library is updated, all the programs using it must be recompiled in order to take advantage of the new code. When a program uses a shared library instead, the program binary does not include a copy of the code, but only a reference to the library. The run time loader, ld.so, finds the library and loads it into memory at the same time as the program.

How they're named, version numbers, symbolic links.


The run time loader finds the library by its "soname" which includes only the major version number (for example, "libfoo.so.1"). Therefore, a new version of the library can be installed, and existing programs will use it automatically. Of course, it is critical to change the major version number if calling sequences change in an incompatible way. Several libraries with different major version numbers can be installed at once, and in fact need to be, until all programs using the library have been recompiled. ldconfig creates a symbolic link with the soname pointing to the current version of the shared library. There should also be a symbolic link with no version number (for example, "libfoo.so") which is used at compile time to find the current version.

libfoo.sa: what "exported initialized library data" means. What flags to compile with. Debugging symbols. Where they're installed.
The shared library ("libfoo.so.1.0.1") and the symbolic link with only the major version number ("libfoo.so.1") should be in /lib if the library is required by any program in /bin or /sbin. The link with no version number ("libfoo.so") is used only at compile time. It should be in /usr/lib.

How ld, ldconfig, ldd, and ld.so find them, and /etc/ld.so.conf.
At compile time, ld searches for shared libraries in standard directories and in directories added with -rpath dir and -Ldir command line options. During system boot, or when run manually (after updating libraries), ldconfig searches in standard places plus directories specified in /etc/ld.so.conf, and saves references to the libraries it finds in a cache. ld.so (at program run time) and ldd (as requested) look in directories specified by the -rpath option given at compile time, or in the cache built by ldconfig.

What "ldconfig -p", "ldconfig -D", nm and ldd can tell you.
"ldconfig -p" will display the contents of the cache. "ldconfig -D" will search for shared libraries and display them. "nm --dynamic /usr/lib/libfoo.so.X" will display the symbols defined by the library. "ldd foo" will display the shared libraries required by foo, and where they were found.

Example commands to build a shared library.


gcc -shared -Wl,-soname,libjvc.so.1 -o libjvc.so.1.0.1 jvc.o ln -sf libjvc.so.1.0.1 libjvc.so.1.0 ln -sf libjvc.so.1.0 libjvc.so.1 ln -sf libjvc.so.1 libjvc.so

Example commands to compile and link using a local shared library.


gcc -I. -o jvct jvct.c -ljvc -L. -Wl,-rpath,`pwd`

[To alter the search path for shared libraries without using -rpath, the ld man page suggests setting LD_RUN_PATH, and the ld.so man page says you set LD_AOUT_LIBRARY_PATH, but I could not get either one to work.]

Example commands to install a shared library.


cp libjvc.so.1.0.1 /lib (cd /usr/lib; ln -sf ../../libjvc.so.1.0.1 libjvc.so) ldconfig

Example commands to compile and link using an installed shared library.


gcc -I. -o jvct jvct.c -ljvc

History: how much more difficult a.out shared libraries were to work with. Pitfalls.
Multiple versions with same name (version scripts?). The libc5/libc6 problem.

Debian Packaging Policy Commentary (The Reasons Behind The Policy).


The policy for packaging libraries is given in /usr/doc/dpkg/packaging.html.

What goes where: static, shared, headers, executables, man pages.


The shared library should go into a package by itself ("libfoo") in section "libs" or "base". A user needing to run but not compile programs need only install that package. Several versions may need to be installed at the same time, to support programs compiled at different times. (This is why the package name may include the major version number.) The symbolic link without the version number, the static library, header files, and documentation go into a second package ("libfoo-dev") in section "devel". Man pages should be installed in section 3 of the manual (/usr/man/man3). These are used by developers. Executables should go into a third package.

Generate your own Library in Linux


Huican Ping Notes Basic knowledge about function? A function is a collection of declarations and statements that carries out a specific action and/or returns a value. Previously defined functions that have related functionality or are commonly used (e.g. math or graphics routines) are stored in object code format in library files. Object code format is a special file format that is generated as an intermediate step when an executable program is produced. Like executable files, object code files are also not displayed to the screen or printed. Functions stored in library files are often called library functions or runtime library routines. The standard location for library file in most Unix system is the directory /usr/lib and /usr/local/lib... How many types libraries do we have and what is the difference between? Usually, two basic types of libraries are used in compilations: static libraries and shared object libraries. Static libraries are collection of object files that are unused during the linking phase of a program. Referenced code is extracted from the library and incorporated in the excutable code . Shared libraries contain re-locatable object that can be shared by more than one application. During compilation, the object code from the library is not incorporated in the executable code, but only a reference to the object is made. When the executable that uses a shared object library is loaded into memory the appropriate shared object library is loaded and attached to the image. What is the libraries name convention? Be convention, the three-letter prefix for a library file is lib and the file extension for a static library is .a. For example, I will show you how you can make a library with name: libmydemo.a later. How can we see the content of a library such as /use/lib/libc.a which is provided by system? Please use this command: $ar t /usr/lib/libc.a | pr -4 -t The unix archieve utility, which creates, modifies and extracts members from an archive, is used above. pr is to display the output to the screen in a fourcolumn format. Additional information can be extracted from library files using the nm utility. For example,

$ nm -C /usr/lib/libstdc++-3-libc6.2-2-2.10.0.a | grep 'bool operator==' will find all the c++ equality operator in the referneced library file. The -C option for nm demangles the complier-generated C++ function names and makes them a bit more readable. Now we will move on to the topic about how can we generated our own library. The ar unility can also be used to create a library. For example, we have 2 functions: ascii which is in ascii.c and change_case which is in change_case.c files. //File: ascii.c char * ascii(int start, int finish) { char *b= new char(finish-start+1); for (int i= start; i<=finish; ++i) b[i-start]=char(i); return b; } //File: change_case.c #include<ctype.h> char* change_case(char *s) { char *t = &s[0]; while(*t) { if(isalpha(*t)) *t+=islower(*t)?-32:32; ++t; } return s; } $ g++ -c change_case.c $ g++ -c ascii.c $ ar cr libmydemo.a ascii.o change_case.o What I did above is the compile the code to object code, and the object code added to the archive with the utility ar. The prototypes for the functions in the mydemo library are placed in a corresponding header file called mydemo.h Be sure to use preprocessor

directives in that file to prevent it from being inadvertently included more than once. //File mydemo.h #ifndef MYDEMO_H #define MYDEMO_H char* ascii(int, int); char* change_case(char*); #endif Well, How to use the library I created? Here is an example: File: main.c #include<iostream> #include"mydemo.h" using namespace std; main() { int start, stop; char b[20]; cout<<"enter the start and stop value for string:"<<endl; cin>>start>>stop; cout<<"create string..."<<ascii(start, stop)<<endl; cin.ignore(80,'\n'); cout<<"enter a string :"; cin.getline(b,20); cout<<"coverted string: "<<change_case(b)<<endl; return 0; } Next step is to compile and link it: $ g++ -o main mani.c -L. -lmydemo Here, "-L." is to use to tell the system that when the compiler searches for library files it should also include the current directory. The name of the library is passed using the -l command option, you call the library without the lib prefix and the .a extension.. Ok, now let's run it? $ main enter the start and stop value for string: 56 68

create string... 89:,0<1321@ABCD enter a string : Hello, World. coverted string: hELLO, wORLD. aha, wonderful!

You might also like