PROCEDURE(), POINTER :: f
CALL C_F_PROCPOINTER(fc, f)
CALL f()
END SUBROUTINE call_sub
-------------------------------------------
And the C code to to test this looks like this:
-------------------C-----------------------------
#include <stdio.h>
void hello(void) {
printf("Hello\n");
}
// Declaration of Fortran function that will make the callback
void call_sub(void(*f)(void));
int main(void) {
call_sub(hello);
}
-----------------------------------------------
I'm using g95 and gcc, but I'm getting a segfault at CALL f(). Am I
doing something wrong in the Fortran code?
Thanks.
It's already beginning to be a common mistake... sort of ISO_C_BINDING
FAQ. It isn't obvious though, I must admit:
SUBROUTINE call_sub(fc) BIND(c)
USE ISO_C_BINDING
TYPE (C_FUNPTR) :: fc
C_PTR (and C_FUNPTR) is an address already. However, Fortran still
has the default of calling (as if) by reference, so the above piece of
code expects a _reference_ to _an address_, which matches a C void**
rather than void*.
Instead, you need:
TYPE (C_FUNPTR), VALUE :: fc
to reduce indirection level by one.
--
Jugoslav
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
Thanks a lot, that did it.