问题描述
我正在使用 GFortran 和 CodeBlocks,但我遇到了关于模块和多个文件的问题.我不断收到此错误:
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
出于某种原因,GFortran 没有构建mesh.mod"文件.当我将所有代码放在一个 .f90 文件中时,不会出现此问题.
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
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
当我使用控制台命令行编译而不是 CodeBlocks 界面时,我已经解决了这个问题(编译多个带有模块的文件),但我仍然遇到 CodeBlocks 的这个问题.
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?
推荐答案
问题是在CodeBlocks中项目是按照出现的顺序构建的,从上到下"(CodeBlocks Wiki),换句话说,文件是按字母顺序编译的.这意味着在我的情况下,Derivatives.f90 比 Main.f90 更早被编译导致错误.
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.
避免该问题的一种方法是仅将 Main.f90
文件设置为 CodeBlocks 中的构建目标:
A way to circumvent the problem is to set only the Main.f90
file as build target in CodeBlocks:
- 菜单
项目/属性...
- 在
Build Target Files
标签中Build targets
勾选 onlyMain.f90
- Menu
Project/Properties...
- In
Build Target Files
at the tabBuild targets
check onlyMain.f90
并在 Main.f90
代码中使用命令 Include 'File_Name.f90'
将其他 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.
这篇关于模块和多个文件的 GFortran 和 CodeBlocks 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!