Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 20.8 Source Code 753


fgets,for example, to return data that was read into a standardI/O buffer 10 minutes
ago if the data was modified by another process 5 minutes ago.
Our discussion of concurrency is predicated on the simple needs of the database
library.Commercial systems often have additional requirements. See Chapter 16 of
Date[ 2004 ]for additional details on concurrency.

20.7 Building the Librar y


The database library consists of two files: a public C header file and a C source file.We
can build a static library using the commands
gcc -I../include -Wall -c db.c
ar rsv libapue_db.a db.o

Applications that want to link with libapue_db.a will also need to link with
libapue.a,since we use some of our common functions in the database library.
If, on the other hand, we want to build a dynamic shared library version of the
database library, we can use the following commands:
gcc -I../include -Wall -fPIC -c db.c
gcc -shared -Wl,-soname,libapue_db.so.1 -o libapue_db.so.1 \
-L../lib -lapue -lc db.o

The resulting shared library,libapue_db.so.1,needs to be placed in a common
directory wherethe dynamic linker/loader can find it. Alternatively, we can place it in
aprivate directory and modify our LD_LIBRARY_PATH environment variable to
include the private directory in the search path of the dynamic linker/loader.

The steps used to build shared libraries vary among platforms. Here, we have shown how to
do it on a Linux system with the GNU C compiler.

20.8 Source Code


We start by showing theapue_db.hheader.This header is included by the library
source code and all applications that call the library.
For the remainder of this text, we depart from the style of the previous examples in
several ways. First, because the source code example is longer than usual, we number
the lines. This makes it easier to link the discussion with the corresponding source
code. Second, we place the description of the source code immediately below the
source code on the same page.

This style was inspired by John Lions in his book documenting the UNIX Version 6 operating
system source code[Lions 1977, 1996].Itsimplifies the task of studying large amounts of
source code.

Note that we do not bother to number blank lines. Although this departs from the
normal behavior of such tools aspr( 1 ), we have nothing interesting to say about blank
lines.
Free download pdf