Renaming a routine in a Fortran module using iso_c_binding

I work with two Fortran modules. The first 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 also contains a routine foothat calls the foofirst module, renamed to 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 them with gfortran to get two object files, and then look into 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

Then I have no problem writing a Fortran program that loads the second module and calls fooinside it ( ___fmod2_MOD_foowhich itself calls ___fmod1_MOD_foo).

My problem arises when I try to do the same from a C program using iso_c_binding. I modify the second module, 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

nm :

fmod1.o:
0000000000000020 s EH_frame1
0000000000000000 T ___fmod1_MOD_foo

fmod2.o:
0000000000000030 s EH_frame1
0000000000000000 T _foo

, . foo C, , foo .

, , ?

+4
2

GCC 79485. (ICE , , , , ). , gfortran , . , , .

+1

BIND (C) , () , ( ).

, " ", . foo ( - .)

, foo , , . , , gfortran, , . .

+2

Source: https://habr.com/ru/post/1669516/


All Articles