Search code examples
typesfortranmexgfortranfortran90

Force explicit variable declaration with gfortran


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.


Solution

    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)