<html> <head> <title>User created libraries/archives</title> <script language="JavaScript"> </script> </head> <body bgcolor="#ffffcc"> <hr> <center> <h1>User created libraries/archives</h1> </center> <hr> <p> Wot is a user library? I hear you ask! Well, its a collection of your functions, that, when placed into a library are available to all the programs you write.<p> This is what <a href="../CONTRIB/SAWTELL/c-lesson.7">Chris Sawtell</a> has to say about libraries. <p> There are two types of library, <a href="dynamic.htm">dynamic</a> and static. Although dynamic libraries are the prefered alternative (because they are not linked into your program), here is the method required to create static libraries.... <p> There are several steps required in creating a library.<p> <ul> <li>Create a function. <li>Create an object module of the function. <li>Add the object module to the library/archive. <li>Compile a program that uses the library/archive. </ul> <hr> Here are the four steps in UNIX terms. The function is called <b>reverse</b> and the library is called <b>mart</b>. <p> <center> <table border=2 bgcolor=ivory> <tr><td> <pre> vi reverse.c # write your function (no main). gcc -c reverse.c # -c just compiles (no link). ar -q libmart.a reverse.o # -r == if nessasary replace # an existing function. gcc program.c -lmart -L/home/leslim # -l == library to search # -L == Location of the library. </pre> </td></tr> </table> </center> <p> A few notes:<p> <ul> <li>reverse.c should NOT contain a <b>main</b>. <li>A header file containing the prototype for reverse.c is required. <li>I expected to be able to add /home/leslim to LD_LIBRARY_PATH but gcc does not seem to look at this variable for its list of libraries. <li>If you dont want to use the <b>-L</B> option you will have to place your library in a directory like <b>/usr/lib</b>. <li>DONT LOOSE THE SOURCE! You cant rebuild the source from the object module. If you want to make a change at a later date you will want the source. </ul> <hr> Other usefull commands: <ul> <li>ar -t libmart.a == List the Objects in a library. </ul> <p> <hr> <h2>See Also:</h2> <ul> <li><a href="syscalls.htm">System calls and Library calls.</a> </ul> <p> <hr> <p> <center> <table border=2 width="80%" bgcolor="ivory"> <tr align=center> <td width="25%"> <a href="../cref.html"> Top</a> </td><td width="25%"> <a href="../master_index.html"> Master Index</a> </td><td width="25%"> <a href="../SYNTAX/keywords.html"> Keywords</a> </td><td width="25%"> <a href="../FUNCTIONS/funcref.htm"> Functions</a> </td> </tr> </table> </center> <p> <hr> <address>Martin Leslie </address><p> </body> </html>