User created libraries/archives
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.
This is what
Chris Sawtell
has to say about libraries.
There are two types of library, dynamic
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....
There are several steps required in creating a library.
- Create a function.
- Create an object module of the function.
- Add the object module to the library/archive.
- Compile a program that uses the library/archive.
Here are the four steps in UNIX terms. The function is called reverse
and the library is called mart.
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.
|
A few notes:
- reverse.c should NOT contain a main.
- A header file containing the prototype for reverse.c is required.
- 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.
- If you dont want to use the -L option you will have to place
your library in a directory like /usr/lib.
- 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.
Other usefull commands:
- ar -t libmart.a == List the Objects in a library.
See Also:
Martin Leslie