C Libraries

-L{path to file containing library} -l${library name}

$ gcc -o myprog myprog.c -L/home/newhall/lib -lmine

If you create your own shared object files and do not install them in /usr/lib, then you need to set your LD_LIBRARY_PATH environment variable so that the runtime linker can find them and load them at run time. For example, if I put my .so files in a directory named lib in my home directory, I'd set my LD_LIBRARY_PATH enviroment to the following:

# if running bash: export LD_LIBRARY_PATH=/home/newhall/lib:$LD_LIBRARY_PATH # if running tcsh: setenv LD_LIBRARY_PATH /home/newhall/lib:$LD_LIBRARY_PATH

USING AND LINKING LIBRARY CODE

To use a Library that is not linked into your program automatically by the compiler, you need to (1) include the library's header file in your C source file (test.c in the example below), and (2) tell the compiler to link in the code from the library .o file into your executable file: step 1: Add an include line (#include "somelib.h") in a program source file (e.g., test.c). step 2: Link the program's .c file with the library object file (i.e. specify the somelib.o file as a command line argument to gcc): % gcc -o myprog test.c somelib.o The resulting executable file (myprog) will contain machine code for all the functions defined in test.c plus any mylib library functions that are called by

CREATING AND USING YOUR OWN LIBRARY CODE

(1) Create an INTERFACE to your library: mylib.h

(2) Create an IMPLEMENTATION of your library: mylib.c

(3) Create a LIBRARY OBJECT FILE (.o) that can be linked with programs that use your library

(3a) or create a SHARED OBJECT FILE (.so) from many .o files that can be dynamically linked with programs that use your library

(3b) or create an ARCHIVE FILE (.a) from many .o files that can be statically linked with programs that use your library

(4) USE the library in other C code: (a) #include "mylib.h" (b) link in the libary code into a.out file

(5) Set LD_LIBRARY_PATH environment variable for finding shared objects in non-standard locations at runtime

Details: