Dear all,
I read a lot of tutorials about calling Fortran functions from C++ code, and I was wondering if it was possible to call a Fortran function from C++ code with pointers arguments that come from Fortran without declaring the pointed type in C++.
Let me explain: I created a derived type in Fortran, which contains a lot of derived types. I would like to be able to insert a layer of C++ between a call from one Fortran subroutine to another, and I would like to avoid to redefine the Fortran derived types in C++, something like:
module my_module implicit none type :: my_type real :: x end type my_type end module my_module subroutine fortran_subroutine(b) use my_module implicit none type(my_type), pointer, intent(in) :: b print *, b%x end subroutine fortran_subroutine program test use my_module implicit none abstract interface subroutine sub (b) import :: my_type type(my_type), pointer, intent(in) :: b end subroutine sub end interface type(my_type), pointer :: a procedure (sub), pointer :: f_ptr => null () procedure (sub) :: fortran_subroutine allocate(a) a%x = 1.0 f_ptr => fortran_subroutine call c_plus_plus_layer(f_ptr,a) end program
extern "C" { void c_plus_plus_layer_(void (*pointer_to_fortran_function)(void **), void ** x); } // Intermediate layer of C++ designed to launch a Fortran subroutine // which doesn't have any argument void c_plus_plus_layer_(void (*pointer_to_fortran_function)(void **), void ** x) { pointer_to_fortran_function(x); }
Thank you for your help !