Executable binaries get functions from static libraries directly into
the executable binary image when the executable is generated through
linking.
Static Libraries are simply collections of object files arranged by an
archieving utility - ar. ar collects all object files into one archieve
file and adds a table that tells which object file in the archieve
define what sysmbols.
The linker, ld, then binds references to a symbol in one object file to
the definitions of that symbol file in the archieve.
Static libraries uses the suffix '.a'.
To create a static library out of a few object files -
ar rcs libname.a file1.o file2.o file3.o ...
Object files can be added one by one as
ar rcs libhello.o file1.o
ar rcs libhello.o file2.o
ar rcs libhello.o file3.o
r - Include the object file to the archieve, replacing any object file
from the archieve with the same name.
c - silently create the library, if it doesnot exist.
s - maintain the table mapping symbol names to object names.
/* ***** hello.c *****/
#include <stdio.h>
void
hello (char *who)
{
printf ("Hello, %s!\n", who);
}
/* **** ****** *****/
$ gcc -c hello.c
$ ls
hello.c hello.o
$ ar cru libhello.a hello.o
$ ranlib libhello.a
$ ls
hello.c hello.o libhello.a
/* ***** main.c ******/
void hello ();
int main (int argc, char *argv[])
{
hello ("World");
exit (0);
}
/* ***** ****** ******/
To link a program against a library
$ gcc -o hello main.c libhello.a
$ ./hello
Hello World!
$ _