I want to expose a C++ class to Fortran. From various searches, I have seen that the most direct way is to use intermediate C functions.
My question is the following: considering a simple C struct describe below and a C function call add
, how can I declare a Fortran module which would declare an associated Fortran derived type and use the add
function for operator overloading ?
struct my_double{ double x; }
my_double add(const my_double*, const my_double*);
The (non working) solution I came up to is something like this:
module my_module
use iso_c_binding
type, bind(c) :: my_double
real(c_double) :: x
end type my_double
interface operator (+)
module procedure add
end interface operator (+)
interface
type(my_double) function add (v1,v2) bind ( c )
use iso_c_binding
type (my_double), intent(in) :: v1,v2
type (my_double) :: v3
end function add
end interface
end module my_module
Does anyone have an advice on how to make this work ?