acosh.o

//Object modules are displayed.

erf.o

fabs.o

. . . .

This indicates that the library was built from object files named acosh.o, erf.o, fabs.o, and so forth. In other words, module names are the same as the names of the object files from which they were created.

Example of Creating an Archive Library

Suppose you are working on a program that does several conversions between English and Metric units. The routines that do the conversions are contained in three C-language files. Following are the three C-language files:

length.c - Routine to Convert Length Units

float

in_to_cm(float in)

/* convert inches to centimeters */

{

 

 

return (in * 2.54);

 

}

 

 

volume.c - Routine to Convert Volume Units

float

gal_to_l(float gal)

/* convert gallons to liters */

{

 

 

return (gal * 3.79);

 

}

 

 

mass.c - Routine to Convert Mass Units

float

oz_to_g(float oz)

/* convert ounces to grams */

{

 

 

 

return (oz * 28.35);

 

}

 

 

During development, each routine is stored in a separate file. To make the routines easily accessible to other programmers, they must be stored in an archive library. To do this, first compile the source files, either separately or together on the same command line:

$ cc -Aa -c length.c volume.c mass.c

Compile them together.

length.c:

 

 

 

volume.c:

 

 

 

mass.c:

 

 

 

$ ls *.o

 

 

List the .o files.

length.o

mass.o

volume.o

 

Then combine the .o files by running ar with the r key, followed by the library name (say libunits.a), followed by the names of the object files to place in the library:

$ ar r libunits.a length.o volume.o mass.o ar: creating libunits.a

To verify that ar created the library correctly, view its contents:

$ ar t libunits.a

Use ar

with the t

key.

length.o

 

 

 

volume.o

 

 

 

mass.o

All the

.o modules

are included; it worked.

Now suppose you have written a program, called convert.c, which calls several of the routines in the libunits.a library. You can compile the main program and link it to libunits.a with the following cc command:

$ cc -Aa convert.c libunits.a

96 Creating and Using Libraries