Jinsong Zhao <
jsz...@yeah.net> wrote:
> On 2012-3-4 1:35, Arjan wrote:
> <snip>
> > works for me now ;-)) ). I imposed a restriction to my source files: I
> > use 1 source file per MODULE and the filename equals the module name:
> > MODULE libxmath can be found in source file libxmath.f90. This
> > convenstion was used by Borland Pascal some decades ago and I have
> > always found it quite intuitive. I regularly swap routines or
> > operators from 1 module to another, or I split some module into 2
> > different ones after a module has grown out of its clothes. Never a
> > problem when I re-compile a program! People interested can have my
> > utility. It's not fool-proof: it's only me who uses it...
> >
> <snip>
>
> I organized my source file in the same way. However, I have encountered
> a difficult problem using this way.
>
> I use one source file per subroutine/function, and put the
> subroutine/function into a MODULE...
[simple example of circular dependence]
Actually, the code exactly as shown also has other problems. The
subroutines are invoked recursively without being declared recursive.
That would be easy to fix. But the recursion is also infinite, which is
a more basic problem. I'll assume that those problems are just artifacts
of attempting to simplify the exposition of the module circularity
problem, and that they have nothing to do with your actual problem. If
your actual code has infinite recursion, then it needs algorithmic
attention rather than just language features. Anyway, ignoring those
problems (which should not normally show up as compilation failures) ...
> Then, the program can't be compiled. How to break the cycle?
It doesn't sound to me like your problem has anything to do with the one
source file per module organization that Arjan describes.
It does, however, look to come from putting one procedure per module.
That is *NOT* "organized...in the same way" as Arjen described, unless I
badly misread his description.
I recommend against that as a general rule. Sure, there are cases where
a module naturally has only a single routine; when that's what is
natural for a particular module, do it. But if you try to make it a
general rule, you will just exacerbate things like the module
circularity problem. In many cases where a set of procedures have
extensive mutual references, those procedures logically go well together
in a single module, which will not then have any problem with circular
module dependency. That's not true in all cases, but it is so in many of
them. I think you are liklely just causing your own problems by forcing
a one-procedure-per-module straightjacket on your code.
There are certainly some cases where grouping multiple procedures into
the same module doesn't work out well as a way to avoid circular
dependency. There are then several approaches.
In my opinion, the best approach is often to take advantage of
submodules. Unfortunately, submodules are likely not supported yet in
many compilers, so that might not be an option for you. Other approaches
often seem like hackish workarounds, I'd agree.
One of the hackish workarounds is to make the relevant procedures
external procedures instead of module ones. Your modules can then just
have interface blocks with interface bodies for the procedures. I really
don't like that approach. But it will work to remove the circular module
dependency.