Fortran子模块编译报#6645错误:模块过程名与作用域名称冲突
Fortran子模块编译符号冲突问题解决
错误原因
你遇到的编译错误不是子模块语法不符合规范,而是代码存在循环依赖导致的符号重复导入冲突,依赖逻辑如下:
- 模块
name1中声明了test2的模块过程接口,所有use name1的作用域都会导入test2这个符号 - 模块
name2中执行了use name1,因此name2的作用域已经包含test2的符号声明 name1的子模块new中又执行了use name2,相当于子模块的作用域提前导入了test2的接口符号,此时你再在子模块中实现test2过程,编译器会判定为同作用域下的同名符号冲突。
修复方案
方案1:解除循环依赖(推荐)
将公共变量z拆分到独立的全局变量模块,避免模块间反向依赖,修改后代码如下:
! 单独存放公共变量的无依赖模块 module global_vars implicit none integer :: z = 22 end module global_vars module name1 implicit none interface module subroutine test2(x) integer,intent(in) :: x end subroutine test2 end interface contains subroutine test1(x) integer :: x call test2(x) end end module name1 module name2 use name1 use global_vars implicit none end module name2 submodule(name1) new use global_vars contains subroutine test2(x) integer,intent(in):: x integer:: y y = 2 print *, x+y+z ,'from test2' end end submodule program name use name2 implicit none call test1(5) end program name
方案2:显式指定导入符号
如果需要保留现有name2的结构,可以在子模块导入name2时只导入需要的变量、排除冲突的test2符号,修改子模块部分即可:
submodule(name1) new use name2, only: z contains subroutine test2(x) integer,intent(in):: x integer:: y y = 2 print *, x+y+z ,'from test2' end end submodule
内容的提问来源于stack exchange,提问作者Eular
相关产品推荐
相关产品推荐

