Datasheet

*/
void bill(char *);
void fred(int);
4. The calling program (program.c) can be very simple. It includes the library header file and
calls one of the functions from the library.
#include <stdlib.h>
#include “lib.h”
int main()
{
bill(“Hello World”);
exit(0);
}
5. You can now compile the program and test it. For now, specify the object files explicitly to the
compiler, asking it to compile your file and link it with the previously compiled object module
bill.o.
$ gcc -c program.c
$ gcc -o program program.o bill.o
$ ./program
bill: we passed Hello World
$
6. Now you’ll create and use a library. Use the ar program to create the archive and add your
object files to it. The program is called
ar because it creates archives, or collections, of individual
files placed together in one large file. Note that you can also use
ar to create archives of files of
any type. (Like many UNIX utilities,
ar is a generic tool.)
$ ar crv libfoo.a bill.o fred.o
a - bill.o
a - fred.o
7. The library is created and the two object files added. To use the library successfully, some sys-
tems, notably those derived from Berkeley UNIX, require that a table of contents be created for
the library. Do this with the
ranlib command. In Linux, this step isn’t necessary (but it is harm-
less) when you’re using the GNU software development tools.
$ ranlib libfoo.a
12
Chapter 1: Getting Started
47627c01.qxd:WroxPro 9/28/07 8:56 PM Page 12