使用gfortran在Fortran 90中编写和调用纯子例程 [英] Writing and calling pure subroutines in Fortran 90 using gfortran

查看:169
本文介绍了使用gfortran在Fortran 90中编写和调用纯子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gfortran编写和调用Fortran 90中的纯子例程时,如何找出编译器发出此错误的原因?

 错误:在(1)处调用XXXX的子程序不是纯粹的

问题尽可能具体,而同时又足够普遍,对其他人有用,所以我会避免粘贴在我的实际代码中,而是会描绘发生的事情。



<我明白在Fortran 90中有各种关于纯程序的规则,我认为这些规则基本归结为不允许在函数或子程序中使用副作用,并且不允许更改使用 intent声明的子例程参数(在)。我有一系列的子程序,最初没有被声明为纯粹的,并且其参数没有被声明的意图,但是它没有执行副作用。首先,我将所有参数声明更改为明确声明的意图,即, out 或<$ c $中的 C> INOUT 。然后,我宣布所有子例程为 PURE 。自然地,第一次尝试时出现了很多错误,但编译器告诉我错误是什么(例如 intent(in)这样的参数正在被修改,例如),所以我一一解决了他们。


然而,在这些程序中有调用,所以现在我仍然收到上面显示的表单的许多错误:子程序调用XXXX 1)不是PURE 。我不明白的是为什么这个调用不是纯粹的。我已经尽了一切所能想到的将XXXX变成纯粹的,但编译器仍然认为它不是。



所以我的问题 - 反思 - 是:如何我是否得到gfortran告诉我为什么它认为XXXX不是纯粹的?

解决方案

将所有PURE子例程放在库I中正在工作,进入一个MODULE(我的客户代码然后USEd)......不知道为什么.....,但做完这些后,出现了更多有用的错误消息,这些错误消息让我能够追踪剩余的杂质。



将子例程放入模块中,然后使用它们使界面变得清晰。这允许编译器检查调用和子例程之间的协议,并在出现差异时生成错误消息。非常有用,因此将子例程和函数放入模块的良好实践中。

使接口显式化的另一种方法是编写接口,但这是额外的工作和额外的步骤出错。



纯子例程/函数有很长的需求列表。如果您有Metcalf,Reid和Cohen的Fortran 95/2003解释,请参见第6.10节。例如,没有保存变量,没有停止语句,外部文件上没有IO,...

您也可以尝试其他编译器来查看它们的错误消息是更有帮助。其他免费的,包括g95和Sun Studio,取决于操作系统。

When writing and calling pure subroutines in Fortran 90 using gfortran, how do I find out why the compiler emits this error?

Error: Subroutine call to XXXX at (1) is not PURE

I'll try to pose my question as specifically as I can while at the same time being general enough to be useful to others, so I'll avoid pasting in my actual code and instead will sketch what happened.

I understand there are various rules about pure procedures in Fortran 90, which I think basically boil down to not permitting side-effects in either functions or subroutines, and not permitting changes to subroutine parameters declared with intent(in). I've a series of subroutines which initially were not declared to be pure, and whose parameters didn't have declared intent, but which nevertheless didn't perform side-effects. First, I changed all parameter declarations to have explicitly-declared intent, either in, out, or inout. Then, I declared all the subroutines to be PURE. Naturally, many errors occurred on the first attempt, but the compiler told me what the errors were (such-and-such parameter with intent(in) is being modified, for example), so one-by-one I fixed them all.

There are calls among these procedures, however, and so now I still get many errors of the form shown above: Subroutine call to XXXX at (1) is not PURE. What I don't understand is why the call is not pure. I've done everything I can think of to make XXXX pure, but the compiler still thinks it isn't.

So my question --rephrased-- is: how do I get gfortran to tell me WHY it thinks XXXX is not pure?

解决方案

"Placed all the PURE subroutines in the library I'm working on, into a MODULE (which my client code then USEd). ...... Not sure why ....., but after doing this more useful error messages appeared which allowed me to track down the remaining impurities."

Placing the subroutines into a module and then using them makes the interface explicit. This allows the compiler to check agreement between the call and the subroutine and generate error messages if there is a discrepancy. Very useful, so placing subroutines and functions into modules good practice.

The other way of making an interface explicit is to write an interface, but that is extra work and an extra step to get wrong.

There is a long list of requirements on pure subroutines/functions. If you have Fortran 95/2003 Explained by Metcalf, Reid and Cohen, see section 6.10. For example, no "save" variables, no stop statement, no IO on external file, ...

You can also try other compilers to see if their error message is more helpful. Other free ones, depending on OS, include g95 and Sun Studio.

这篇关于使用gfortran在Fortran 90中编写和调用纯子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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