Node:Building a library, Next:, Previous:Writing a makefile, Up:Putting a program together



Building a library

We explored what libraries are and how to use them in a previous chapter. (See Libraries, if you need to refresh your memory.) You may have wondered how libraries are written in the first place. Is the whole process too complicated for a mortal C programmer to attempt? Not at all.

Suppose you have a function (or set of functions) that you would like to use widely across the various C programs you write. You might even like to make it available to other users in a convenient way. To create a code library that will enable you to achieve this, follow the sequence below. We will use a code example, but you can create your own library by taking similar steps.

  1. Here's an example of the kind of function you might like to use in multiple programs. It accepts one string containing some text to print, and then prints it on the default printer.

    For the sake of example, the file below is named lpr_print.c.

    #include <stdio.h>
    
    void lpr_print (char *the_text)
    {
      FILE *printer;
    
      printer = popen ("lpr", "w");
      fprintf (printer, the_text);
      pclose (printer);
    }
    

    (See Programming with pipes, for the rationale behind this function.)

  2. Now we will create a library.

  3. Now create a header file that will allow users access to the functions in your library. You should provide one function prototype for each function in your library. Here is a header file for the library we have created, called liblprprint.h.
    /*
       liblprprint.h:
       routines in liblprprint.a
       and liblprprint.so
    */
    
    extern void lpr_print (char *the_text);
    
  4. Now you should put your libraries and include file somewhere your code can access them. For the sake of this example, create the directories include and lib in your home directory. Once you have done so, move the .a and .so files you have created to lib, and the .h file to include.
  5. If you have taken the last step, and you want to run a program linked to a shared version of your library, you should type a line like the following into your shell (the following command line assumes you are using the Bash shell and that your home directory is named /home/fred):
    export LD_LIBRARY_PATH=/home/fred/lib:$LD_LIBRARY_PATH
    

    This command line sets an environment variable that makes the linker search the /home/fred/lib directory before it searches anywhere else. You can include it in your .bashrc or .bash_profile file. If you don't execute this command before you attempt to run a program using your shared library, you will probably receive an error.

  6. Now you can write programs that use your library. Consider the following short program, called printer.c:
    #include <liblprprint.h>
    
    /* To shorten example, not using argp */
    int main ()
    {
      lpr_print ("Hello, Multiverse!\nHowarya?\n");
      return 0;
    }
    
  7. The executable produced is called printer. Try it!

Footnotes

  1. To create library files containing multiple object files, simply include the object files on the same command line. For example, to create a static library with multiple object files, type a command such as ar rs liblprprint.a lpr_print.o lpr_print2.o lpr_print3.o. Similarly, to create a shared library, type gcc -shared -o liblprprint.so lpr_print.o lpr_print2.o lpr_print3.o.