How to call Fortran interface (generic procedure) from C/C++

How to call Fortran interface (generic procedure) from C/C++

I tryed all kind of things and none worked...

May be there is something I'm missing? Is it that difficult to implement?

Here is my C code

int main ( void ) {
   char test [10] = "abcd";
   int t = 4;
   void myfort (int* i);

   myfortsub (test);
   myfortsub2 (&t);

//   Doesn't work!
//   myfort (&t);
   return 0;

My Fortran code

module my
use iso_c_binding
interface myfort
   module procedure myfortsub
   module procedure myfortsub2
end interface myfort

subroutine myfortsub2 ( integ ) bind ( C, name="myfortsub2" )
   use iso_c_binding, only: C_INT
   implicit none
   integer(kind=C_INT), intent (in) :: integ
   write (*, *) ">", integ+1, ">", -1
end subroutine myfortsub2

subroutine myfortsub ( input_string ) bind ( C, name="myfortsub" )
   use iso_c_binding, only: C_CHAR, c_null_char
   implicit none
   character (kind=c_char, len=1), dimension (10), intent (in) :: input_string
   character (len=10) :: regular_string
   integer :: i

   regular_string = " "
   loop_string: do i=1, 10
      if ( input_string (i) == c_null_char ) then
         exit loop_string
         regular_string (i:i) = input_string (i)
      end if
   end do loop_string
   write (*, *) ">", trim (regular_string), "<", len_trim (regular_string)
end subroutine myfortsub
end module my


As you already guessed I'd like to call "myfort" from C - what should I do for?

Thanks in Advance!

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

That's because there is no actual routine named myfort - it's just a generic name which the Fortran compiler resolves to one of the specific names you provided.

Retired 12/31/2016

From C++ you would declare multiple myfortsub's with different argument lists (overloads). These subs would call the specific (named) interface subroutine (function) as done on the IVF side. IOW generic call converted to specific call.

Jim Dempsey


Leave a Comment

Please sign in to add a comment. Not a member? Join today