4

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?

4
  • it's gfortran 5.3.0 Commented Feb 12, 2017 at 16:39
  • Could you try adding bind(c,name="foo") or bind(c,name="foo_f") and see the output of nm foo2.o? (On my computers, the result depends on the version of gfortran...) Commented Feb 12, 2017 at 16:40
  • For bind(c,name="foo") I get the same problematic results, while for bind(c,name="foo_f") I get the correct result: fmod2.o: 0000000000000030 s EH_frame1 U ___fmod1_MOD_foo 0000000000000000 T _foo_f Commented Feb 12, 2017 at 16:50
  • 1
    Same for me with gfortran6.3 on mac10.11. Interestingly, bind(c) (as in the question) works with older gfortran4.8 (on a Linux box). On the other hand, ifort-16 and oracle studio fortran 12.5 work for all patterns. ... hmm Commented Feb 12, 2017 at 16:56

2 Answers 2

2

When you add BIND(C) to the procedure, you are specifying (indirectly) the binding name instead of the compiler applying its own rules (that include the module name).

It's not that "the second module no longer seems to require the first module" but that you've changed the binding name of the routine in the second module. You haven't touched the binding name of foo in the first module (which is not its local name due to the rename.)

That said, the compiler ought to know the binding name of foo in the first module, referenced through its local name, and put out the correct name in the object for the call. From what other commenters have said, the version of gfortran you're using may have a bug here. Try a newer one.

Sign up to request clarification or add additional context in comments.

1 Comment

There are several similar problems already reported by me. They started in gfortran 4.9 but have not been fixed yet. gcc.gnu.org/bugzilla/show_bug.cgi?id=66695 gcc.gnu.org/bugzilla/show_bug.cgi?id=77746 It may all have a common origin.
1

This is now GCC Bug 79485. I have already reported very similar and very probably related bugs before (ICE with binding-name equal to the name of a use-associated procedure and Wrong subroutine called, clash of specific procedure name and binding-name). Unfortunately the gfortran developers are only a few and very busy and did not fix this problem yet. If they see other people encountering it they might consider it with a slightly higher priority.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.