This is a basic into on making libraries for a unix/linux machine using
standard gnu tools.
Static libraries
The things you need to know when compiling a static library are :
- You compile each object file normally
- Use ar to put the object file(s) into the library. This typically
invoked as "ar rcv" with the objects.
- The standard naming convention is libxxx.a, where xxx is the name
of your library.
The things you need to know when using a static library are :
- When you compile your code, use the -I flag to give header file
location(s).
- When you link your code, use the -L flag to give library
location(s). And use the -lxxx flag to link your library, where xxx is
the name of your library, this must be after the object files in the
link command.
Shared libraries
The things you need to know when compiling a shared library are :
- You compile each object file with the -fpic flag. This tells the
compiler to make it position independant. This is needed since the
addresses will be physical not relative when the code is run.
- Use gcc -shared to put the object file(s) into the
library.
- The standard naming convention is libxxx.so, where xxx is the name
of your library.
The things you need to know when using a shared library are :
- When you compile your code, use the -I flag to give header file
location(s).
- When you link your code, use the -L flag to give library
location(s). And use the -lxxx flag to link your library, where xxx is
the name of your library, this must be after the object files in the
link command. In a Makefile LDLIBS and LOADLIBES are the standard
symbols used for this.
- When you run the program, you will need to have the library path
known to the linker. This can be done by adding the path to /etc/ld.so.conf,
setting LD_LIBRARY_PATH to contain your library path, or installing the
library in /lib or /usr/lib ( or /usr/local/lib ).
If I can't install the library, I usually write a shell script to set
LD_LIBRARY_PATH for me and run the program.
#!/bin/sh
export LD_LIBRARY_PATH=.
./$@