I am working with two Fortran modules. The first one contains a subroutine foo:
module fmod1
contains
subroutine foo(i)
implicit none
integer, intent(inout) :: i
i=i+1
end subroutine foo
end module fmod1
The second one also contains a subroutine called foo which calls the foo of the first module, renamed as foo_first:
module fmod2
use fmod1, only : foo_first => foo
contains
subroutine foo(i)
implicit none
integer, intent(inout) :: i
i=i+2
call foo_first(i)
end subroutine foo
end module fmod2
When I compile these with gfortran to get two object files and then look inside them with nm, I see the expected result:
fmod1.o:
0000000000000020 s EH_frame1
0000000000000000 T ___fmod1_MOD_foo
fmod2.o:
0000000000000030 s EH_frame1
U ___fmod1_MOD_foo
0000000000000000 T ___fmod2_MOD_foo
I then have no problem in writing a Fortran program which loads the second module and calls the foo within it (___fmod2_MOD_foo, which itself calls ___fmod1_MOD_foo).
My problem comes when I try to do the same thing from a C program using iso_c_binding. I change the second module by adding bind(c) to the subroutine:
module fmod2
use iso_c_binding
use fmod1, only : foo_first => foo
contains
subroutine foo(i) bind(c)
implicit none
integer, intent(inout) :: i
i=i+2
call foo_first(i)
end subroutine foo
end module fmod2
Running nm again on the object files now gives this:
fmod1.o:
0000000000000020 s EH_frame1
0000000000000000 T ___fmod1_MOD_foo
fmod2.o:
0000000000000030 s EH_frame1
0000000000000000 T _foo
i.e., the second module no longer seems to require the first module. When I try experimenting with calling foo from the second module from a C program it becomes apparent that the subroutine, instead of calling foo from the first module, is calling itself in an infinite loop.
Is this a bug, or am I doing something which I shouldn't be doing?