I trying to learn how to write C-interoperable Fortran procedures. I guess, I got the gist of it for normal type arguments, but I am somewhat lost for procedure arguments.
Let us suppose the following procedure with an explicit interface:
real(dp) function averagefnc(fnc, a, b) result(res)
!! Average of function with explicit interface
interface
function fnc(x)
import dp
real(dp), intent(in) :: x
real(dp) :: fnc
end function
end interface
real(dp), intent(in) :: a, b
res = (fnc(a) + fnc(b))/2
end function
Could the C-interoperable Fortran procedure look like so?
real(c_double) function averagefnc_c(fnc, a, b) result(res) bind(c)
interface
function fnc(x) bind(c)
import c_double
real(c_double), intent(in) :: x
real(c_double) :: fnc
end function
end interface
real(c_double), intent(in) :: a, b
res = averagefnc(fnc, a, b) ! something is still missing here, I imagine
end function
Or is it necessary to transform the explicit interface into an abstract interface and do as described here: Working with C Pointers (The GNU Fortran Compiler)?
Any hints on documentation and/or an open source project that makes use of such features are also very welcome.