Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

fortran - GFortran and CodeBlocks issue with Modules and Multiple Files

I am working with GFortran and CodeBlocks but I'm having an issue about Modules and Multiple files. i keep getting this error:

Fatal Error: Can't open module file 'mesh.mod' for reading at (1): No such file or directory

For some reason, GFortran is not building the 'mesh.mod' file. This problem does not occur when I put all the code in a single .f90 file.

Bellow is an example of code that this error happens.

main.f90

MODULE MESH
IMPLICIT NONE
INTEGER :: IMAX,JMAX,NMAX
REAL(8), ALLOCATABLE :: XD(:),YD(:),FX(:,:),FY(:,:)
REAL(8) :: PI,E,DX,DY,H,L,RHO,MU
PARAMETER (PI = ACOS(-1.D0))
PARAMETER (E = 2.718)
END MODULE MESH
!**************************************************************

program Cavity
Use Mesh
implicit none
Real(8), Allocatable :: func(:)
Real(8) :: Der,DfDx
integer :: i

IMAX=10
DX=1./10

Allocate(xd(IMAX),func(IMAX))

Do i=1,IMAX
   xd(i)=i*DX
End Do

Do i=1,IMAX
func(i) = xd(i)**2
End Do

Der=Dfdx(func,2)
Write(*,*) Der

End program Cavity

Derivatives.f90

Real(8) Function DfDx(f,i)
        Use Mesh
        implicit none
        Real(8) :: f(1:Imax)
        integer :: i

           DfDx=(f(i+1)-f(i-1))/(2d0*dx)

        return
end function DfDx

When I use console command line compilation instead of CodeBlocks interface I already solved this problem (Compiling Multiple Files with modules) but I'm still getting this problem with CodeBlocks.

Does anyone know how to solve this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming what you have written is how your code is, then it appears that the problem is that the module mesh is inside the main program and not a separate file. You should have three files: Mesh.f90, Derivatives.f90 and Main.f90.

Mesh.f90 is exactly as you have it,

module Mesh
   implicit none
   integer :: IMAX,JMAX,NMAX
   real(8), allocatable :: XD(:),YD(:),FX(:,:),FY(:,:)
   real(8) :: PI,E,DX,DY,H,L,RHO,MU
   parameter (PI = ACOS(-1.D0))
   parameter (E = 2.718)
end module Mesh

Derivatives.f90 should be written as another module, using contains:

module Derivatives
   use mesh
  contains
   real(8) function dfdx(f,i)
      real(8) :: f(i:imax)
      integer :: i

      DfDx=(f(i+1)-f(i-1))/(2d0*dx)

   end function dfdx
end module Derivatives

and the Main.f90 will then use both modules. Note that I had to eliminate the variable DfDx; this is because it conflicts with the function DfDx in module Derivatives

program Cavity
   Use Mesh
   use Derivatives
   implicit none
   Real(8), Allocatable :: func(:)
   Real(8) :: Der
   integer :: i  

   IMAX=10
   DX=1./10

   Allocate(xd(IMAX),func(IMAX))

   Do i=1,IMAX
      xd(i)=i*DX
   End Do

   Do i=1,IMAX
      func(i) = xd(i)**2
   End Do

   Der=Dfdx(func,2)
   Write(*,*) Der

End program Cavity

I do not know how CodeBlocks works, but I would presume it lets you choose the compilation order. If that is the case, you should compile Mesh.f90 first, then Derivatives.f90, then compile Main.f90 before linking them to an executable.

When I compiled & linked them, I got an answer of 0.200000002980232; hopefully that links up to what you have as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...