Novak Elliott wrote:
> i need a few pointers for writing static (.a) and dynamic link libraries
> (.so). I saw once in a makefile the command "ar myfile.c ..." to build a
> static library ie mylib.a .
A static library is just an `ar' archive of .o files. You generally
just use
ar r libfoo.a file1.o file2.o ...
ranlib libfoo.a
ranlib creates an index of symbols, which speeds up linking.
Dynamic libraries are a bit more involved.
First, all source files need to be compiled with the -fPIC switch to
create position-independant (relocatable) code.
Then, to create the shared library itself, use e.g.
gcc -shared -o libfoo.so.1.2 -Wl,-soname,libfoo.so.1 file1.o file2.o ...
The `-Wl,-soname,libfoo.so.1' sets the library's `soname' to
libfoo.so.1. When you link a program with a shared library, the soname
is embedded into the executable (this is the information that `ldd'
displays).
When you run the program, the run-time linker (ld-linux.so) uses the
soname to find the appropriate libraries. For each library, it checks
that the soname embedded in the library matches the one embedded in
the executable. This prevents programs from using an incompatible
version of a shared library.
The soname usually includes the major version number of the library,
but not any minor version numbers, as versions of a library which have
the same major number are supposed to be binary compatible.
E.g. libc.so.5.4.23 and libc.so.5.4.38 both have the soname
`libc.so.5'. Any program which was linked with one of these libraries
can use any version which has the same soname.
To link against a library (either shared or static), either:
1. Use -l<libname>, where libname is the name of the library minus the
`lib' prefix and any `.so' or `.a' suffix. E.g. to link with
libX11.so, use `-lX11'. If the library isn't in a standard library
directory (typically /lib, /usr/lib and /usr/local/lib), you also need
to use `-L <directory>'.
2. Specify the full pathname to the library as if it were an object
file, e.g.
gcc -o myprog file1.o file2.o ... /usr/local/mylibs/libfoo.a
NB: For static libraries, you have to specify the library *after* any
object files which use functions defined within it.
gcc processes the files on the command line in the order that they are
specified. When gcc encounters a static library, it only links in the
object files from the library which are needed to resolve symbols
which have been used by files that have already been processed.
--
Glynn Clements <[EMAIL PROTECTED]>