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.
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 (); }
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 func3The #! 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.
cc -c share1.c cc -c share2.c cc -o shrsub.o share1.o share2.o -bE:shrsub.exp -bM:SRE -bnoentryThis 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.
ar qv libsub.a shrsub.oThis 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.
cc -o main main.c -lsub -L/home/sharedlibIf the shared library is not in an archive, use the command:
cc -o main main.c /home/sharedlib/shrsub.o -L/home/sharedlibThe 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.
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.