Libraries |
4 |
![]() |
There are two basic kinds of software libraries:
There are two advantages to the use of libraries:
LD_OPTIONS.Using
LD_OPTIONS environment variable:
demo% setenv LD_OPTIONS "-m -Dfiles" demo% f77 -o myprog myprog.f |
demo% f77 -o myprog -Qoption ld -m -Qoption ld -Dfiles myprog.f |
Some linker options have their compiler command-line equivalents and can appear directly on the f77 or f90 command:
-Bx, -dx, -G, -hname, path
-R, and -ztext. Generating a Load Map
The linker -m option generates a load map that displays library linking information listing the routines linked during the building of the executable binary program. Routines are listed together with the libraries that they come from.-m for load map:
Listing Other Information
Solaris 2.3 and later has additional linker debugging features, available through the linker's -Dkeyword option. A complete list can be displayed using -Dhelp.
For example, the -Dfiles linker option lists all the files and libraries referenced during the link process:
See the Linker and Libraries Guide for further information on these linker options.
Consistent Compiling and Linking
Ensuring a consistent choice of compiling and linking options is critical whenever compilation and linking are done in separate steps. Compiling any part of a program with any of the following options requires linking with the same options:
demo% f77 -c -a sbr.f demo% f77 -c smain.f demo% f77 -a sbr.o smain.o {pass -a to the linker}
|
Library Search Paths and Order
The linker searches for libraries at several locations and in a certain prescribed order. Some of these locations are standard paths, while others depend on the compiler options -Rpath, -llibrary and -Ldir and the environment variable LD_LIBRARY_PATH. Search Order for Standard Library Paths
The standard library search paths used by the linker are determined by the installation path, and they differ for static and dynamic loading.
|
|
Standard Install |
Nonstandard Install to /my/dir/ |
|
BaseDir = |
/opt/SUNWspro/ |
/my/dir/SUNWspro/ |
Static Linking
While building the executable file, the static linker searches for any libraries in the following paths (among others), in the specified order:
-Rpath
-llibrary compiler option to name additional libraries for the linker to search when resolving external references. For example, the option -lmylib adds the library libmylib.so or libmylib.a to the search list.The linker looks in the standard directory paths to find the additional libmylib library. The
-L option (and the LD_LIBRARY_PATH environment variable) creates a list of paths that tell the linker where to look for libraries outside the standard paths. Were libmylib.a in directory /home/proj/libs, then the option
-L/home/proj/libs would tell the linker where to look when building the executable:
demo% f77 -o pgram part1.o part2.o -L/home/proj/libs -lmylib |
Command-Line Order for
For any particular unresolved reference, libraries are searched only once, and only for symbols that are undefined at that point in the search. If you list more than one library on the command line, then the libraries are searched in the order they are found on the command line. Place -llibrary options as follows:-llibrary Options
-Ldir Options
-Ldir option adds the dir directory path to the library search list. The linker searches for libraries first in any directories specified by the -L options and then in the standard directories. This option is useful only if it is placed preceding the -llibrary options to which it applies.
LD_LIBRARY_PATH Environment Variable
LD_LIBRARY_PATH to specify directory paths the linker should search for libraries specified with the -llibrary option. Using this environment variable would make the previous example look like:
demo% setenv LD_LIBRARY_PATH /home/proj/libs demo% f77 -o pgram part1.o part2.o -lmylib |
;dirlist2
-Ldir directories specified on the command line, followed then by dirlist2 and the standard directories.That is, if the compiler is called with any number of occurrences of
-L, as in:
LD_LIBRARY_PATH variable contains only a single colon-separated list of directories, it is interpreted as dirlist2.
Note - Use of this environment variable with production software is strongly discouraged. Although useful as a temporary mechanism for influencing the runtime linker's search path, any dynamic executable that can reference this environment variable will have its search paths altered, which could cause unexpected results or a degradation in performance.
-Rpath option. (Contrast with the -Ldir option which indicates where at buildtime to find the library specified by a -llibrary option, but does not record this path into the binary executable.)The directory paths that were built in when the executable was created can be viewed using the dump command.
Example: List the directory paths built into a.out:
demo% f77 program.f -R/home/proj/libs -L/home/proj/libs -lmylib demo% dump -Lv a.out | grep RPATH [5] RPATH /home/proj/libs:/opt/SUNWspro/lib |
ld.so: prog: fatal: libmylib.so: can't open file:The possible causes might be:
demo% ldd a.out
libsolib.so => /export/home/proj/libsolib.so
libF77.so.3 => /opt/SUNWspro/lib/libF77.so.3
libc.so.1 => /usr/lib/libc.so.1
libdl.so.1 => /usr/lib/libdl.so.1
LD_LIBRARY_PATH is not set correctly.
LD_LIBRARY_PATH at runtime includes the path to the needed libraries.
The linker extracts from the library any elements whose entry points are referenced within the program it is linking, such as a subprogram, entry name, or COMMON block initialized in a BLOCKDATA subprogram. These extracted elements (routines) are bound permanently into the a.out executable file generated by the linker.
demo% f77 main.f -lsunperf crunch.f -o myprog (Incorrect)
demo% f77 main.f crunch.f -lsunperf -o myprog (Correct)
Suppose further that the files are organized in such a way that they each contain a single principal subprogram that would be called by the user program, along with any "helper" routines that the subprogram may call but which are called from no other routine in the library. Also, any helper routines called from more than one library routine are gathered together into a single source file. This gives a reasonably well-organized set of source and object files.
Assume that the name of each source file is taken from the name of the first routine in the file, which in most cases is one of the principal files in the library:
demo% cd test_lib demo% ls total 14 2 dropx.f 2 evalx.f 2 markx.f 2 delte.f 2 etc.f 2 linkz.f 2 point.f |
The lower-level "helper" routines are gathered together into the file etc.f. The other files may contain one or more subprograms.
-c option, to generate the corresponding relocatable .o files:
Now, create the static library testlib.a using ar:
demo% ar cr testlib.a *.o |
To use this library, either include the library file on the compilation command or use the
-l and -L compilation options.
demo% cat trylib.f C program to test testlib routines x=21.998 call evalx(x) call point(x) print*, 'value ',x end demo% f77 -o trylib trylib.f test_lib/testlib.a trylib.f: MAIN: demo% |
In the preceding example, grep finds entries in the list of names only for those library routines that were actually called.
-llibrary and -Lpath options. Here, the library's name would have to be changed to conform to the libname.a convention:
demo% mv test_lib/testlib.a test_lib/libtestlib.a demo% f77 -o trylib trylib.f -Ltest_lib -ltestlib trylib.f: MAIN: |
demo% f77 -o myprog myprog.f -L/usr/local/lib -ltestlib |
Replacement in a Static Library
It is not necessary to recompile an entire library if only a few elements need recompiling. The -r option of ar permits replacement of individual elements in a static library.
demo% f77 -c point.f demo% ar r testlib.a point.o demo% |
Ordering Routines in a Static Library
To order the elements in a static library when it is being built by ar, use the commands lorder(1) and tsort(1):
demo% ar cr mylib.a 'lorder exg.o fofx.o diffz.o | tsort' |
Creating Dynamic Libraries
Dynamic library files are built by the linker ld from precompiled object modules that can be bound into the executable file after execution begins.
-pic or -PIC).In position-independent code, each reference to a global item is compiled as a reference through a pointer into a global offset table. Each function call is compiled in a relative addressing mode through a procedure linkage table. The size of the global offset table is limited to 8Kbytes on SPARC processors. The
-Bdynamic | -Bstatic
-Bdynamic sets the preference for shared, dynamic binding whenever possible. -Bstatic restricts binding to static libraries only.
-dy | -dn
-dy allows dynamic, shared libraries to be linked. -dn does not allow linking dynamic libraries.
The linker also accepts an optional version number suffix: for example, libmyfavs.so.1 for version one of the library, etc.
The compiler's
-hname option records name as the name of the dynamic library being built.
-G, -ztext, and -hname. These linker options are available through the compiler command line.You can create a dynamic library with the same files used in the static library example.
Example: compile with -pic and other linker options
:
-G tells the linker to build a dynamic library.
Note that the example uses the
-R option to bind into the executable the path (the current directory) to the dynamic library.
Libraries Provided with Sun Fortran Compilers
Table 4-1 shows the libraries are installed with the compilers:
See also the math_libraries README file for more information.
VMS Library
The libV77 library is the VMS library, which contains two special VMS routines, idate and time. POSIX Library
There are two versions of POSIX bindings provided with the compilers:
-lFposix)
-lFposix_c)
Both POSIX libraries come in static and dynamic forms.
The POSIX bindings provided are for IEEE Standard 1003.9-1992.
IEEE 1003.9 is a binding of 1003.1-1990 to FORTRAN (X3.8-1978).
POSIX.1 documents:
|
Standard install |
/opt/SUNWspro/READMEs/runtime.libraries |
|
Install to /my/dir/ |
/my/dir/SUNWspro/READMEs/runtime.libraries |
Do not redistribute or otherwise disclose the header files, source code, object modules, or static libraries of object modules in any form.