ChorusOS 5.0 Application Developer's Guide

Dynamic Link of a Shared Library at Application Start-up

A small modification to the previous example enables a shared application to load a shared library dynamically.

Loading a Shared Library Dynamically
  1. Create the shared library file.

    Copy the libdyn.c file into the libshareddir directory and rename it libshared.c. The ensuing Imakefile will build a PIC binary object file called libshared.o and a shared library named libshared.so as follows:

    FPIC = ON
    SRCS = libshared.c
    SharedLibraryTarget (libshared.so, libshared.o, , , , )
    Depend($(SRCS))

  2. Create the shared program file.

    Copy the progdyn.c file into the progshareddir directory and rename it progshared.c. The ensuing Imakefile will build a PIC binary object file called progshared.o and an executable file called progshared:

    FPIC = ON
    SRCS = progshared.c
    LIBPATH = <pathname of the shared library directory>
    SharedUserTarget (progshared_u, progshared.o,
       $(UTILS_LIB) $(CLX_UTILS_LIB), -L$(LIBPATH)
       -Xlinker -rpath -Xlinker/shared -lshared,,)
    Depend($(SRCS))

  3. Copy the shared application.

    Copy the shared application into the /bin subdirectory of the chorus_root_directory directory:


    % cp $WORK/progshareddir/progshared_u chorus_root_directory/bin 
    

  4. Copy the shared library.

    Copy the shared library into the /shared subdirectory of the chorus_root_directory directory:


    % cp $WORK/libshareddir/libshared.so chorus_root_directory/shared
    


    Note -

    There is no need to set LD_LIBRARY_PATH as the path for the runtime linker. This path was specified in the Imakefile by setting the -Xlinker -rpath option.


  5. Start the application.

    The following command starts the application and loads the libshared.so and libc.so libraries (if they have not already been loaded by another application):


    % rsh jericho /bin/progshared_u