1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
<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>
|