Fortran callback to C 
Author Message
 Fortran callback to C

I'm trying to pass a function pointer from C to fortran and have
Fortran call back to that function.  My Fortran code looks like this:
--------------Fortran----------------------
! Callback to a C function with no return value and no arguments
SUBROUTINE call_sub(fc) BIND(c)
  USE ISO_C_BINDING
  TYPE (C_FUNPTR) :: fc

  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");

Quote:
}

// Declaration of Fortran function that will make the callback
void call_sub(void(*f)(void));

int main(void) {
  call_sub(hello);

Quote:
}

-----------------------------------------------

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.



Tue, 06 Sep 2011 08:54:51 GMT  
 Fortran callback to C

Quote:

> I'm trying to pass a function pointer from C to Fortran and have
> Fortran call back to that function.  My Fortran code looks like this:
> --------------Fortran----------------------
> ! Callback to a C function with no return value and no arguments
> SUBROUTINE call_sub(fc) BIND(c)
>   USE ISO_C_BINDING
>   TYPE (C_FUNPTR) :: fc

>   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?

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.



Tue, 06 Sep 2011 17:18:25 GMT  
 Fortran callback to C

Quote:

> > I'm trying to pass a function pointer from C to Fortran and have
> > Fortran call back to that function. ?My Fortran code looks like this:
> > --------------Fortran----------------------
> > ! Callback to a C function with no return value and no arguments
> > SUBROUTINE call_sub(fc) BIND(c)
> > ? USE ISO_C_BINDING
> > ? TYPE (C_FUNPTR) :: fc

> > ? 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?

> 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.

> --
> Jugoslavwww.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.


Tue, 06 Sep 2011 21:52:54 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. to CS: or not to CS: in F-PC assembler

2. FORTRAN as First Language for CS Majors?

3. Calling C callback function from Fortran g77

4. Callback in Fortran DLL to a VB routine ...

5. Trying the Fortran to VB Callback but ...

6. Callback function Fortran-Delphi

7. CallBack functions in Fortran DLLs

8. Camp Smalltalk (CS 6) registration

9. APL as First Language for CS

10. SoftEng or CS Graduate Program Search

11. Test and Set (TS) vs Compare and Swap (CS)

 

 
Powered by phpBB® Forum Software