Reputation: 27
I want to do something like this:
main.f90 uses module A and module B.
module_A.f90 is independent.
module_B.f90 uses module A.
My code is like this:
main.f90
include "module_A.f90"
include "module_B.f90"
program MAIN
use A
use B
write(*,*) Array(I)
end program MAIN
module_A.f90
module A
integer, parameter :: I = 10
end
module_B.f90
include "module_A.f90"
module B
use A
real*8 :: Array(I)=1d0
end module B
However, the error information shows:
Error: module_A name 'module_A' at (1) is already being used as a MODULE at (2)
It seems that I can't use a module to cite another module. Is there any way to solve this problem?
(Note: it is not the problem of missing "contains" because it doesn't involve subroutine or function)
Upvotes: 1
Views: 2780
Reputation: 929
when you included modA.f90 and modB.f90 in main.f90, since you already included modA.90 in modB.90, then your main.f90 look like this:
module modA
...
end module
module modA
...
end module
module modB
use mod A
...
end module
program
...
end program
Since in fortran you can't have two unitwith the same name, the compiler fails with the error you see because you declared two module modA.
modA.f90
module modA.f90
...
end module
modB.90
module modB
use modA
...
end module
main.f90
program main
use modB
use modA
...
end program
However use
statement needs to deals with dependencies, so modA.90
must be compiled before modB.f90
gfortran -o main modA.f90 modB.90 main.f90
I do not recommend this solution, but for the sake of completness, it is provided.
modA.f90
module modA.f90
...
end module
modB.90
module modB
use modA
...
end module
main.f90
include modA.f90
include modB.f90
program main
use modB
use modA
...
end program
In this case, since include
is like copy-pasting, we only need to compile the program
gfortran -o main main.f90
modA.f90
module modA.f90
...
end module
modB.90
include modA.f90
module modB
use modA
...
end module
Since modA is already included, no need to do it again
main.f90
include modB.f90
program main
use modB
use modA
...
end program
Again we only need to compile the program
gfortran -o main main.f90
Upvotes: 3