Compiling and Executing FORTRAN Programs Requiring Subprograms

Programs can reference subprograms (subroutines or functions) that are contained in the same file as the main calling program or are external to the main program in separate files or in libraries.

When the subprograms are in the same file as the main program, use the compilation procedure described in the tip sheet "Compiling and Executing a Stand-alone Program" .

Basic compilation and execution

If the subprograms are in separate files, compile the main calling program and the subprograms with the following command.

   f77 [main] [sub1] [sub2] .. [subN]   -o [output filename]
where

For example, if the main program in "main.f" calls subroutines in files "one.f" and "two.f" and a function subprogram in "func.f", then compile each unit and produce an executable file named "main.exe" by typing

   f77 main.f one.f two.f func.f   -o main.exe
To execute the program, type

   main.exe

Detailed explanation of the compilation process

In the previous example, this procedure produces five output files:

   main.o  one.o  two.o  func.o  main.exe
The ".o" files are the relocatable binaries, which cannot be used separately, but must be linked together to create an executable file. The f77 command performs this step by calling the link editor and producing the executable file "main.exe".

Alternatively, you can compile each subprogram separately, producing individual ".o" files. Use the -c compiler switch to produce each ".o" file. For example,

   f77 -c main.f
   f77 -c one.f
   f77 -c two.f
   f77 -c func.f
The final step is to link them together to make the executable file "main.exe".

   f77 main.o one.o two.o func.o   -o main.exe

Using subprograms in a user-written subroutine library

This section pertains to libraries of subprograms written by individuals, rather than IT-supported libraries such as IMSL and NAG. Use of the IMSL and NAG is discussed in the tip sheet "1.3. Using IMSL and NAG Fortran Subroutine Libraries." Four alternatives are presented: the first two have all the information entered on the "f77" command and the second two have the location of the library stored in an environment variable named LD_LIBRARY_PATH. Directions for creating these libraries are in the tip sheet "Creating Subprogram Libraries".


Alternative 1:

The name of the file that contains the library may have any name. For example, it might be named "mysubs" in the directory ~/mylib (i.e., $HOME/mylib).

Specify the full filename for the library in the compile statement. For example,

   f77 main.f   ~/mylib/mysubs    -o main.exe
or

   f77 main.f one.f two.f func.f  ~/mylib/mysubs   -o main.exe

Alternative 2:

The name of the file that contains the library MUST have the prefix "lib" and the suffix ".a", For example, it might be named "libmysubs.a" in the directory ~/mylib (i.e., $HOME/mylib).

Use the "-L" option to specify the location of the library in the compile statement. For example,

   f77 main.f -o main.exe -L$HOME/mylib   -lmysubs
or

   f77 main.f one.f two.f func.f   -L$HOME/mylib    -lmysubs
Even though the full filename is "libmysubs.a", only type the "mysubs" part with the "-l" option.

Alternative 3:

The name of the file that contains the library must have the prefix "lib" and the suffix ".a". For example, it might be named "libmysubs.a". This alternative does not use the "-L" compiler option. You MUST store the file in the directory ~/lib ($HOME/lib). You must also use the recommended, IT-supported startup files, "dotfiles" (.cshrc, .login, .localenv, .localalias). If you are not, you can make an automatic backup of your current "dotfiles" and replace them with the current versions by typing the following command at the UNIX prompt:

   ~consult/proto/xsetup
If you have defined your own aliases or environment variables, you will need to copy them into the new "dotfiles". Instructions are available on U-Discover!.

Compile your program by typing

   f77 main.f -o main.exe   -lmysubs
or

   f77 main.f one.f two.f func.f     -lmysubs

Alternative 4:

This is the same as alternative 3 except that the library does not need to be in the directory named ~/lib ($HOME/lib). It uses the LD_LIBRARY_PATH environment variable to specify the directory containing the library.

For example, suppose the library file is named "libmysubs.a" and is stored in ~/mylib ($HOME/mylib). First, add the following line to the .localenv file described in alternative 3:

   setenv LD_LIBRARY_PATH $HOME/mylib:$LD_LIBRARY_PATH
Then compile your program by typing

   f77 main.f -o main.exe -lmysubs 
                                                           
                                                                
 *******************************************************************
 *                                                                 *
 *                   Correspondence to VS FORTRAN                  *
 *                                                                 *
 *******************************************************************
 *                                                                 *
 *       VS FORTRAN                      UNIX                      *
 *       ==========                      ====                      *
 *                                                                 * 
 *                    Compile, Load & Execute                      *
 *                    =======================                      *
 *                                                                 * 
 * fortvs2 main                   f77 main.f                       *
 * fortvs2 sub                    f77 sub.f                        *
 * load main sub (start           f77 main.o sub.o -o main.exe     *
 *                                main.exe                         *
 *                                                                 *
 *                                          or                     *
 *                                                                 *
 *                               f77 main.f sub.f -o main.exe      *
 *                               main.exe                          *
 *                                                                 *       
 *                                                                 *       
 *            Accessing User Created Library Routines              *   
 *            =======================================              *  
 *                                                                 *       
 * global txtlib vsf2fort mysubs                                   *   
 * fortvs2 main           f77 main.f mylib/libmysubs.a -o main.exe *   
 * load main (start       main.exe                                 *  
 *                                                                 *    
 *                                         or                      *    
 *                                                                 *    
 *                  f77 main.f -o main.exe -L$HOME/mylib -lmysubs  *
 *                  main.exe                                       *   
 *                                                                 * 
 *                      where the subprograms in the library       *
 *                      libmysubs.a  are located in the            *
 *                      directory named $HOME/mylib.               *
 *                                                                 *       
 *                    (See text for alternative methods)           *
 *                                                                 *       
 *******************************************************************

University of Delaware
June 19, 1994