Fortran dependency cycle on submodules
On both Make and Ninja it seems we cannot write module + submodule in the same file without having a circular dependency.
Consider the following modules/submodules
!! mod_a.f90
module mod_a
implicit none
private
public :: value_a, add_b
interface
module function value_a() result(res)
integer :: res
end function
module function add_b(a) result(res)
integer, intent(in) :: a
integer :: res
end function
end interface
end module mod_a
!! mod_b.f90
module mod_b
implicit none
private
public :: value_b, add_a
interface
module function value_b() result(res)
integer :: res
end function
module function add_a(b) result(res)
integer, intent(in) :: b
integer :: res
end function
end interface
end module mod_b
!! main.f90
program run
use mod_a
use mod_b
implicit none
print *, "b + a = ", add_b(value_a())
print *, "a + b = ", add_a(value_b())
end program run
!> mod_a_impl.f90
submodule (mod_a) impl
use mod_b
implicit none
contains
module function value_a() result(res)
integer :: res
res = 1
end function
module function add_b(a) result(res)
integer, intent(in) :: a
integer :: res
res = a + value_b()
end function
end submodule impl
!> mod_b_impl.f90
submodule (mod_b) impl
use mod_a
implicit none
contains
module function value_b() result(res)
integer :: res
res = 2
end function
module function add_a(b) result(res)
integer, intent(in) :: b
integer :: res
res = b + value_a()
end function
end submodule impl
We have 3 outcomes from it:
- Combine all into 1 file: Success
- Split all into 5 files (
main.f90
,mod_a.f90
,mod_a_impl.f90
,mod_b.f90
,mod_b_impl.f90
): Success - Split into 3 files (
main.f90
,mod_a.f90
,mod_b.f90
): Failure:# Ninja: Generating Fortran dyndep file CMakeFiles/run.dir/Fortran.dd ninja: build stopped: dependency cycle: fortran_mods/mod_a.mod -> fortran_mods/mod_b.mod -> fortran_mods/mod_a.mod. # Make: Building Fortran object CMakeFiles/run.dir/src/split/mod_b.f90.o f951: Fatal Error: Module file ‘mod_a.mod’ has not been generated, either because the module does not contain a MODULE PROCEDURE or there is an error in the module. compilation terminated.
.o
files, even as the modules themselves allow it.
Edited by Cristian Le