[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
General Programming Concepts: Writing and Debugging Programs

Creating a Shared Library

Prerequisite Tasks

  1. Create one or more source files that are to be compiled and linked to create a shared library. These files contain the exported symbols that are referenced in other source files.

    For the examples in this article, two source files, share1.c and share2.c, are used. The share1.c file contains the following code:

    /************
     * share1.c: shared library source.
    *************/
     
    #include <stdio.h>
     
    void func1 ()
     {
       printf("func1 called\n");
     }
     
    void func2 ()
     {
       printf("func2 called\n");
     }
    The share2.c file contains the following code:
    /************
     * share2.c: shared library source.
    *************/
     
    void func3 ()
     {
       printf("func3 called\n");
     }
    The exported symbols in these files are func1, func2, and func3.
  2. Create a main source file that references the exported symbols that will be contained in the shared library.

    For the examples in this article the main source file named main.c is used. The main.c file contains the following code:

    /************
     * main.c: contains references to symbols defined
     * in share1.c and share2.c
    *************/
     
    #include <stdio.h>
     
      extern void func1 (),
                     unc2 (),
                     func3 ();
    main ()
     {
                     func1 ();
                     func2 ();
                     func3 ();
     }
  3. Create the exports file necessary to explicitly export the symbols in the shared library that are referenced by other object modules.

    For the examples in this article, an exports file named shrsub.exp is used. The shrsub.exp file contains the following code:

    #! /home/sharelib/shrsub.o 
    * Above is full pathname to shared library object file
    func1
    func2
    func3
    The #! line is meaningful only when the file is being used as an import file. In this case, the #! line identifies the name of the shared library file to be used at run time.

Procedure

  1. Compile and link the two source code files to be shared. (This procedure assumes you are in the /home/sharedlib directory.) To compile and link the source files, enter the following commands:
    cc -c share1.c
    cc -c share2.c
    cc -o shrsub.o share1.o share2.o -bE:shrsub.exp -bM:SRE -bnoentry
    This creates a shared library name shrsub.o in the /home/sharedlib directory.
    -bM:SRE flag Marks the resultant object file shrsub.o as a re-entrant, shared library

    Each process that uses the shared code gets a private copy of the data in its private process area.

    flag Sets the dummy entry point _nostart to override the default entry point, _start
    -bnoentry flag Tells the linkage editor that the shared library does not have an entry point

    A shared library may have an entry point, but the system loader does not make use of an entry point when a shared library is loaded.

  2. Use the following command to put the shared library in an archive file:
    ar qv libsub.a shrsub.o
    This step is optional. Putting the shared library in an archive makes it easier to specify the shared library when linking your program, because you can use the -l and -L flags with the ld command.
  3. Compile and link the main source code with the shared library to create the executable file. (This step assumes your current working directory contains the main.c file.) Use the following command:
    cc -o main main.c -lsub -L/home/sharedlib
    If the shared library is not in an archive, use the command:
    cc -o main main.c /home/sharedlib/shrsub.o -L/home/sharedlib
    The program main is now executable. The func1, func2, and func3 symbols have been marked for load-time deferred resolution. At run time, the system loader loads the module in to the shared library (unless the module is already loaded) and dynamically resolves the references.
-L flag Adds the specified directory (in this case, /home/sharedlib) to the library search path, which is saved in the loader section of the program.

At run time the library search path is used to tell the loader where to find shared libraries.

LIBPATH environment variable A colon-separated list of directory paths that can also be used to specify a different library search path. Its format is identical to that of the PATH environment variable.

The directories in the list are searched to resolve references to shared objects. The /usr/lib and /lib directories contain shared libraries and should normally be included in your library search path.

Related Information

The Shared Library Overview .

The ar command, as command, cc command, ld command.

The load subroutine, loadquery subroutine, loadbind subroutine, unload subroutine.

The XCOFF object (a.out) file format.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]