10.1.1. Include Files

Up: Contents Next: Error Checking Previous: Differences between PETSc Interfaces for C and Fortran

PETSc Fortran users have two choices for including the PETSc header files.

Recommended Approach: In the first approach, the Fortran include files for PETSc are located in the directory ${}PETSC_DIR/include/finclude and should be used via statements such as the following:

    #include "include/finclude/includefile.h" 
Since one must be very careful to include each file no more than once in a Fortran routine, application programmers must manually include each file needed for the various PETSc components within their program. This approach differs from the PETSc C/C++ interface, where the user need only include the highest level file, for example, snes.h, which then automatically includes all of the required lower level files. As shown in the examples of Section Sample Fortran77 Programs , in Fortran one must explicitly list each of the include files. If using this approach one must employ the Fortran file suffix .F rather than .f. This convention enables use of the CPP preprocessor, which allows the use of the #include statements that define PETSc objects and variables. (Familarity with the CPP preprocessor is not needed for writing PETSc Fortran code; one can simply begin by copying a PETSc Fortran example and its corresponding makefile.)

Alternative Approach: If working with .f files is absolutely essential (perhaps as part of a heritage code), the conventional Fortran style include statement can be employed. The weakness of this approach is that either the complete path of the include file must be hardwired with a statement such as

   include '/home/username/petsc/include/foldinclude/includefile.h' 
or a link must be estabilished in the directory containing the Fortran source file to the file
   ln -s /home/username/petsc/include/foldinclude/includefile.h includefile.h 
Some Fortran compilers will accept a -I<directory>, but depending on the Fortran compiler, they may use the -I list only for the #include style of include. In addition, the user must declare all PETSc objects as integer rather than by their name. For example, declarations within Fortran .F files have the form
   SLES    solver 
   Mat     A, B 
   Vec     x, y 
   integer i 
while the analogous statements within .f files are
   integer solver 
   integer A, B 
   integer x, y 
   integer i 


Up: Contents Next: Error Checking Previous: Differences between PETSc Interfaces for C and Fortran