如何在Fortran95中链接主程序和子程序? [英] how to link main and subprograms in Fortran95?

查看:91
本文介绍了如何在Fortran95中链接主程序和子程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将外部子例程链接到Fortran中的主程序?虽然,我从书中得到的答案是"Fortran 90 for Scientists and Engineer"(科学家和工程师专用的Fortran 90)如下:

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:

还请注意,由于外部子程序位于单独的位置主程序中的文件,则必须单独编译.在下面FTN90一种中间类型的机器代码,称为可重定位二进制文​​件在扩展名为.OBJ的文件中生成.反过来这必须是通过称为a的特殊程序与调用程序链接链接器,最终生成主程序的.EXE版本.你的编译器手册将提供有关如何执行此操作的详细信息.一旦是最终调试完毕,无需重新编译外部子程序,仅链接.这样可以避免您浪费时间进行编译而且,如果它是一个内部子程序,则将是这种情况.

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.

无论如何,我没有找到任何有关如何链接主程序和子程序的手册.我使用Silverfrost(Plato)调试和运行程序.此外,我已经在系统上安装了"Intel Parallel Studio XE 2011".

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.

推荐答案

暂时,我将假定外部子程序(函数或子例程)与主程序位于同一目录中.在fortran77或更早的时代,您只需从主程序中调用子例程,并将它们与主程序和子程序中都存在的COMMON语句链接即可.但是,如今,首选的方法是将您的外部子程序写入模块,并使用USE语句链接该模块.例如,让我们组成一个虚构的子程序,该子程序读取值"x"并对其进行处理以将值"y"和"z"反馈给主程序.该程序和模块可能类似于以下示例.从本质上讲,fortran模块试图确保某种面向对象,因为它可以确保将数据封装到程序中,除非程序员希望将其全局化,否则无法全局访问.重要的是,程序员只能将子程序声明为PRIVATE,如果该子程序仅由模块中的其他程序调用,则将其声明为PUBLIC(如果要由主程序调用).同样,为了确保正确使用数据,如果变量仅被馈送到例程中,不在例程中或将被称为tot,则应将其声明为INTENT(IN),INTENT(OUT)或INTENT(INOUT).他例行公事,从事工作并反馈到主程序.我希望这有帮助,即使不是随意提出任何其他问题的话.同样,当使用外部子程序编译主程序时,请按照要编程的子程序的顺序调用它们.这意味着不要使用ifort main.f90 module.f90,而是必须在使用intel编译器的情况下键入fort module.f90 main.f90,否则请使用用于编译器的任何命令替换fort.

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

这篇关于如何在Fortran95中链接主程序和子程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆