问题
This problem has been around for a few years, but I never heard of a good solution to it.
If a Fortran subroutine includes a module with an interface to itself
subroutine stuff(nz,z,dt)
use allinterfaces
...
an error occurs
Error: 'stuff' of module 'allinterfaces', imported at (1), is also the name of the current program unit
To fix it I have to declare
subroutine stuff(nz,z,dt)
use allinterfaces, except_this_one => stuff
...
This is absurd behavior and annoying since I like to include all interfaces in a module. It would be helpful if this was not a compile error, or at least there should be an except_itself
The compiler I am using is gfortran version 4.8.2 (GCC), but I doubt this is the compiler's fault.
Does anyone understand the rational behind this behavior, or a more practical solution?
The compiler could use this information to check whether the interface defined in the module matches the actual subroutine. So it's two levels worse than it could be. Ignoring the information is a missed opportunity; treating it as an error is counterproductive.
回答1:
This behavior is specified by the Fortran standard. The standards committee discussed relaxing the restriction, termed "interface to self", but ultimately rejected it. I don't remember the specifics. Since the standard forbids it, compilers are required to be able to diagnose it and most do by default.
You'd encounter this problem only when trying to incrementally update an F77-style program to use explicit interfaces. I'll note that Intel Fortran, and maybe gfortran as well, has a feature that will automatically check such interfaces for you (in ifort it's -warn interface).
Perhaps the better approach is to put your procedures in modules.
来源:https://stackoverflow.com/questions/32161560/fortran-interface-to-itself-produces-annoying-error