How to link an external subroutine to the main program in Fortran? Although, I got an answer from the book with the title of "Fortran 90 for scientists and engineers" as following:
Note also that, since an external subprogram resides in a separate file from the main program, it must be compiled separately. Under FTN90 an intermediate type of machine code, called relocatable binary is produced in a file with the .OBJ extension. This in turn must be linked with the calling program by means of a special program called a linker, finally resulting in a .EXE version of the main program. Your compiler manual will have the details of how to do this. Once it is finally debugged, an external subprogram need never be recompiled, only linked. This prevents you from wasting time in compiling it over and over, which would be the case if it was an internal subprogram.
Anyway I did not find any manual for how to link the main and subroutine programs. I use Silverfrost (Plato) to debug and run the programs. Besides, I have installed the "Intel Parallel Studio XE 2011" on the system.
For the time being I am going to assume that the external subprogram (function or subroutine) is in the same directory as the main program. In the days of fortran77 or earlier you would just call the subroutine from the main program and link them with a COMMON statement that existed both in the main program and the subprogram. However, now days the preferred method is to write your external subprograms into modules and link the module with a USE statement. For example lets make up a fictional subprogram that read in a value of 'x' and worked on it to feed back values of 'y' and 'z' to the main program. The program and module might look like the example below. A Module in essence i fortran attempt at becoming somewhat object oriented as it ensures that data is encapsulated to the programs and is not globally accessible unless the programmer wants it to be global. It is important that the programmer declare a subprogram as PRIVATE, if it is only to be called by other programs in the module and PUBLIC if it is to be called by the main program. Also in order to ensure that data is used properly, you should declare it as INTENT(IN), INTENT(OUT) or INTENT(INOUT) if the variable is only being fed into the routine, out of the routine or will be called tot he routine, worked on and fed back to the main program. I hope this helped, if not feel free to ask any other questions. Also when compiling a main program with external subprograms call them in order of subprograms to program. This means do not use ifort main.f90 module.f90, instead you must type fort module.f90 main.f90 assuming you are using an intel compiler, if not then replace fort with whatever command is used for your compiler.
PROGRAM MAIN
USE Test
IMPLICIT NONE
REAL :: X,Y,Z
X = 5.0
WRITE(*,*) X,Y,Z
END PROGRAM MAIN
MODULE Test
PUBLIC :: Subroutine_Example
CONTAINS
SUBROUTINE Subroutine_Example(X,Y,Z)
REAL, INTENT(IN) :: X
REAL, INTENT(OUT :: Y,Z
Y = X + 34.6
Z = X - 1.4
END Subroutine_Example
END MODULE Test