The example below shows a sample Fortran main program using free source form that uses a module and an external subprogram.
The function CALC_AVERAGE is contained in a separate file and depends on the module ARRAY_CALCULATOR for its interface block.
The USE statement accesses the module ARRAY_CALCULATOR. This module contains the function declaration for CALC_AVERAGE.
The 5-element array is passed to the function CALC_AVERAGE, which returns the value to the variable AVERAGE for printing.
The example is:
! File: main.f90 ! This program calculates the average of five numbers PROGRAM MAIN USE ARRAY_CALCULATOR REAL, DIMENSION(5) :: A = 0 REAL :: AVERAGE PRINT *, 'Type five numbers: ' READ (*,'(F10.3)') A AVERAGE = CALC_AVERAGE(A) PRINT *, 'Average of the five numbers is: ', AVERAGE END PROGRAM MAIN
The example below shows the module referenced by the main program. This example program shows more Fortran 95/90 features, including an interface block and an assumed-shape array:
! File: array_calc.f90. ! Module containing various calculations on arrays. MODULE ARRAY_CALCULATOR INTERFACE FUNCTION CALC_AVERAGE(D) REAL :: CALC_AVERAGE REAL, INTENT(IN) :: D(:) END FUNCTION CALC_AVERAGE END INTERFACE ! Other subprogram interfaces... END MODULE ARRAY_CALCULATOR
The example below shows the function declaration CALC_AVERAGE referenced by the main program:
! File: calc_aver.f90. ! External function returning average of array. FUNCTION CALC_AVERAGE(D) REAL :: CALC_AVERAGE REAL, INTENT(IN) :: D(:) CALC_AVERAGE = SUM(D) / UBOUND(D, DIM = 1) END FUNCTION CALC_AVERAGE
During the early stages of program development, the sample program files shown above might be compiled separately and then linked together, using the following commands:
Linux OS and Mac OS* X example:
ifort -c array_calc.f90
ifort -c calc_aver.f90
ifort -c main.f90
ifort -o calc main.o array_calc.o calc_aver.o
Windows* example:
ifort /c array_calc.f90
ifort /c calc_aver.f90
ifort /c main.f90
ifort /exe:calc main.obj array_calc.obj calc_aver.obj
In this sequence of commands:
Line 1: The -c (Linux OS and Mac OS X) or /c (Windows OS) option prevents linking and retains the object files. This command creates the module file array_calculator.mod and the object file array_calc.o (Linux OS and Mac OS X) or array_calc.obj (Windows OS). Note that the name in the MODULE statement determines the name of module file array_calculator.mod. Module files are written into the current working directory.
Line 2: This command creates the object file calc_aver.o (Linux OS and Mac OS X) or calc_aver.obj (Windows OS).
Line 3: This command creates the object file main.o (Linux OS and Mac OS X) or main.out (Windows OS)and uses the module file array_calculator.mod.
Line 4: This command links all object files into the executable program named calc. To link files, use the ifort command instead of the ld command.
If your path definition includes the directory containing calc, you can run the program by simply entering its name:
calc
When running the sample program, the PRINT and READ statements in the main program result in the following dialogue between user and program:
Type five numbers: 55.5 4.5 3.9 9.0 5.6 Average of the five numbers is: 15.70000
To debug a program with the debugger, compile the source files with the -g (Linux OS and Mac OS X) or /debug:full (Windows OS) option to request additional symbol table information for source line debugging in the object and executable program files.
The following ifort command lines for Linux OS and Mac OS X systems use the -o option to name the executable program file calc_debug. The Mac OS X command line also uses the -save-temps option, which specifies that the object files should be saved; otherwise, they will be deleted by default.
ifort -g -o calc_debug array_calc.f90 calc_aver.f90 main.f90 (Linux) ifort -g -save-temps -o calc_debug array_calc.f90 calc_aver.f90 main.f90 (Mac OS X)
The Windows OS equivalent of this command is the following:
ifort /debug:full /exe:calc_debug array_calc.f90 calc_aver.f90 main.f90
See also Debugging Fortran Programs and related sections.
Copyright © 1996-2010, Intel Corporation. All rights reserved.