Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

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

Anar_Yusifov
Beginner
289 Views

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!

0 Kudos
2 Replies
Steven_L_Intel1
Employee
289 Views

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.

0 Kudos
jimdempseyatthecove
Honored Contributor III
289 Views

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

 

0 Kudos
Reply