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

contains
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
   return
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
      else
         regular_string (i:i) = input_string (i)
      end if
   end do loop_string
   write (*, *) ">", trim (regular_string), "<", len_trim (regular_string)
   return
end subroutine myfortsub
end module my

<!--break-->

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

Thanks in Advance!

3 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

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.

Steve

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

 

www.quickthreadprogramming.com

Melden Sie sich an, um einen Kommentar zu hinterlassen.