next up previous
Next: Shared Library Up: Unix & Linux Previous: dump the obj file

Static Library(archive)

Static library (archive) is actaully a collection of the object files. To create the archive file, use the ar command in linux
ar -rsc archive object1 object2
more detail

Each archive starts with the magic eight character string !<arch>; Archive includes the symbol directory, by which the linker can find the member position by the names. Once created, this index is updated in the archive whenever ar makes a change to its contents (save for the q update operation). An archive with such an index speeds up linking to the library, and allows routines in the library to call each other without regard to their placement in the archive.

For example, libfoo.a is the archive of the tow object files foo.o and bar.o, which contain the function foo and function bar respectively. The size of the three files is

-rw-------  1 welu students  731 Oct  8 21:43 bar.o
-rw-------  1 welu students  727 Oct  8 21:43 foo.o
-rw-------  1 welu students 1676 Oct  8 21:45 libfoo.a
Now if there is a main.c file which invokes the foo(), we can link the library by running
g++ -o main main.o -lfoo -L./
And the generated executable size is much bigger,
-rwx------  1 welu students 7963 Oct  8 21:45 main
-rw-------  1 welu students 1056 Oct  8 21:28 main.o
That means the executable statically at least includes the main.o as well as the libfoo.a file. If running ldd, the result showes the libfoo.a is not a part of the dependent libraries of the executable
linux-gate.so.1 =>  (0xffffe000)
libstdc++.so.5 => /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.5-20050130/libstdc++.so.5 (0xb7f04000)
libm.so.6 => /lib/libm.so.6 (0xb7ee2000)
libgcc_s.so.1 => /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.5-20050130/libgcc_s.so.1 (0xb7eda000)
libc.so.6 => /lib/libc.so.6 (0xb7dcb000)
/lib/ld-linux.so.2 (0xb7feb000)
So precisely speaking, archive is the non-shareable static library;


next up previous
Next: Shared Library Up: Unix & Linux Previous: dump the obj file
Wei Lu 2007-11-06