用gfortran强制显式变量声明 [英] Force explicit variable declaration with gfortran

查看:176
本文介绍了用gfortran强制显式变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mex连接matlab中的一些Fortran代码(f90),偶尔会有matlab冻结。

过去,由于不匹配,我发生冻结之间的数据类型(如整数* 4与整数* 8)。

我链接的代码有许多隐式定义的变量,所以我想知道是否有隐藏的数据类型冲突只能偶尔出现。为了排除数据类型不匹配的原因,我希望编译器要求显式声明所有变量。



问题:


  1. 如何获得gfortran要求全部变量在编译时显式声明?否则,有什么办法可以至少得到警告?


  2. 是否所有体系结构中的gfortran都将真实数据类型解释为特定类型?如果是这样,哪一个是它(真实* 4,真实* 8,...)?

  3. 是否有强迫gfortran解释真实数据类型是一种特定的类型,比如说real * 4?


  4. 关于什么让fortran代码在从mex编译的例程中调用时冻结在MATLAB中(数据类型不匹配除外)?


    感谢您的帮助。

    在我弄清楚之前,我会经历许多代码行,试图列出所有隐式定义的变量。
    不用说,我会非常感谢任何人把我从这样无聊的任务中解救出来......


    Best,

    G

    解决方案


    1. 声明为通过添加隐式无

    2. 我相信默认的真实数据类型是 real * 4

    3. 您可以使用命令行标记 -fdefault-real-8 来强制所有变量声明为真实被解释为 real * 8

    注意(为了编写更多的代码,不一定要解决当前的错误):
    如果您使用的是Fortran 90代码,则可以使用 real (kind = 4) real(kind = 8) with gfortran而不是 real * 4 real * 8 语法。我已经不再使用命令行标志设置实数或整数大小,而是使用整数,参数:: REAL_SIZE 变量来保存适当的数字(我通常会去对于4或8,因为我使用的所有编译器都支持它们,但是如果你想变得非常便携,你应该使用 selected_real_kind 例程)

    I am linking some fortran code (f90) from matlab using mex and I am having matlab freeze occasionally.

    In the past, I had freezing happening due to mismatch between data types (say integer*4 vs integer*8).

    The code I am linking has many implicitly defined variables, so I am wondering if there is a hidden data type conflict that only occurs occasionally.

    To rule out data type mismatch as the cause of the freeze, I would like to have the compiler requiring all variables to be explicitly declared.

    Questions:

    1. How do I get gfortran to require all variables to be explicitly declared at compile time? Failing that, is there any way to at least get warnings?

    2. Is a "real" data type interpreted by gfortran as a specific kind in all architectures? If so, which one is it (real*4, real*8, ...)?

    3. Is there anyway to force gfortran to interpret the "real" data type as a specific kind, say "real*4"?

    4. Any ideas on what makes the fortran code to freeze when called from a mex compiled routine in matlab (other than data type mismatches)?

    Thanks for any help.

    Until I figure this out I will be going through many lines of codes trying to list all implicitly defined variables. Needless to say, I will be tremendously grateful to anyone who frees me from such a boring task...

    Best,

    G.

    解决方案

    1. You can require all variables to be explicitly declared by adding implicit none.
    2. I believe the default "real" data type is a real*4.
    3. You can use a command-line flag -fdefault-real-8 to force all variables declared as real to be interpreted as a real*8

    Note (for writing more code, not necessarily trying to solve the current bug): If you're using Fortran 90 code, you can use real(kind=4) or real(kind=8) with gfortran rather than the real*4 or real*8 syntaxes. I've moved away from setting the real or integer size using command-line flags and instead use an integer, parameter :: REAL_SIZE variable to hold the appropriate number (I typically go for 4 or 8 because all the compilers I use support them, but if you want to be very portable you should use the selected_real_kind routine)

    这篇关于用gfortran强制显式变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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