Creating FORTRAN Libraries

On UDelVM, the "txtlib" command is used to create libraries of subprograms. On UNIX, the same thing can be accomplished using the "ar" command. The format for this command is

   ar cr [libname] [routine1.o] ...... [routinen.o]
or in verbose mode,

   ar crv [libname] [routine1.o] ...... [routinen.o]
For example,

   ar crv libmysubs.a one.o two.o three.o 
will create a library of routines named libmysubs.a.


Example:

Given six subprograms (i.e., subroutines and/or functions): one.f, two.f, three.f, four.f, five.f and six.f, you want to combine these into a library.

  1. Use the f77 command to compile each routine to create its object files.

    	f77 -c one.f 
    	f77 -c two.f 
        .
    	f77 -c six.f
    
    or

    	f77 -c one.f two.f three.f four.f five.f six.f
    
  2. Use the ar command to create a library named libmysubs.a.
    	ar crv libsubsmy.a one.o two.o three.o four.o five.o six.o 
    

    Efficient ordering of libraries

    On Strauss, the link editor "ld" can make multiple passes over a library and therefore does not require any special ordering of the library routines. In the previous example, suppose all routines except one and two are independent, but routine "two" calls routine "one". If the routines are listed in the "ar" command as shown, and if a main calling program called subroutine "two", then two passes over the library would be required. Only one pass would be neeeded if the library were ordered two.o one.o, three.o, four.o, five.o, six.o.

    Strauss' link editor can make the multiple passes needed to resolve all of the subprogram references, so the library is now ready to use on Strauss. However, to create the library with more efficient ordering, use the routines "lorder" and "tsort". For this example, you would type

    	ar crv libmysubs.a `lorder one.o two.o ... six.o | tsort`
    For small libraries such at this one with only six routines, the difference in load time is negligible. However, for larger libraries, the lorder command may allow for an efficient access of the library during the link edit process.


    Use "lorder" and "tsort" rather than "ranlib"

    On Brahms and Chopin, the link editor "ld" cannot make multiple passes over a library and the special ordering of the library routines IS required. Although an alternative to "lorder" is available on these three systems through the use of "ranlib, we recommend against using "ranlib since it is not available for Strauss.

    If main.f is a program that calls one or more of the routines in libmysubs.a, AND libmysubs.a is in the same directory as main.f, you can compile and link all the routines by adding libmysubs.a to the f77 command:

    	f77 main.f   libmysubs.a   -o main.exe
    This only works if libmysubs.a is in the directory that contains main.f. If it is not, you must either specify the full pathname for the library, use the -L option, or use the environment variable LD_LIBRARY_PATH. For a description of these choices see the tip sheet "Compiling and Executing FORTRAN Programs Requiring Subprograms".


    Updating libraries

    If you modify one of the routines in the library, you can update the library using the "r" (replace) option. If, you modify "five.f" in the example above, then you can replace the old version of the subprogram with the new one by typing

    	f77 -c five.f
    	ar r libmysubs.a  five.o
    
    Note that there is no "-" sign preceding the "r" option.

    Use the "u" options to replace all routines in a library that have changed since they were put into the library. You can also use the "v" option to list the names of the replaced routines. For example, to update subprograms stored in "two.f" and "four.f", type

    	f77 -c two.f  
    	f77 -c four.f
    	ar uv libmysubs.a
    
    This works only if the all routines that went into making the library are in the current directory. Otherwise, you must list the routines explicitly. For example, if routines two.o, four.o and five.o have been changed and these routines are in the current directory, then update the library by typing

    	ar uv libmysubs.a
    If six.o is in subdirectory "sub", then type the following instead:

    	ar uv libmysubs.a sub/six.o

                                                                    
    *********************************************************************
    *                                                                   *
    *                Correspondence to VS FORTRAN                       *
    *                                                                   *
    *********************************************************************
    *                                                                   *
    *             VS FORTRAN                      UNIX                  *
    *             ==========                      ====                  *
    *                                                                   *
    *                   Creating a Subprogram Library                   *
    *                   =============================                   *
    *                                                                   *
    *  txtlib gen libmy fn1 fn2....    ar crv libmysubs.a               *
    *                                    `lorder sub1.o sub2.o | tsort` *                       *                                                                   *
    *                                                                   *
    *                        Updating the Library                       *
    *                        ====================                       *
    *                                                                   *
    *  txtlib del libmy sub                ar rv libmysubs.a sub.o      *
    *  txtlib add libmy sub                                             *
    *                                                                   *
    *   where file "sub text a"             where file "sub.o"          *
    *   contains the old version of         contains the new version    *
    *   the subprogram when the             of the subprogram and       *  
    *   "txtlib del" command is issued      replaces the old version in *
    *   and contains the revised            the library "libmysubs.a".  *
    *   version when the "txtlib add"                                   *
    *   command is issued.                                              *
    *                                                                   *
    *********************************************************************
    

    University of Delaware
    June 19, 1994