可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
On codeblock, you may go to Project properties > Build targets Then select the file you want to build first (say mod.f90). In the "Selected file properties" go to "Build" Here,change the priority weight. Lower weight implies the file will be built first.
回答3:
The problem is that in CodeBlocks "projects are built in the order of appearence, from top to bottom" (CodeBlocks Wiki), in other words, the files are compiled alphabetically. Which means that in my case, Derivatives.f90 was being compiled before than Main.f90 causing the error.
A way to circumvent the problem is to set only the Main.f90
file as build target in CodeBlocks:
- Menu
Project/Properties...
- In
Build Target Files
at the tab Build targets
check only Main.f90
And use the command Include 'File_Name.f90'
inside the Main.f90
code to include the other f90
files for compilation in the right order.