[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Passing functions from C++ DLD to Fortran and vise versa
From: |
John W. Eaton |
Subject: |
Passing functions from C++ DLD to Fortran and vise versa |
Date: |
Wed, 6 Jun 2007 18:13:26 -0400 |
On 6-Jun-2007, Thomas Treichl wrote:
| I've a (hopefully simple) problem that I can't figure out. I'm trying to pass
a
| function pointer from C++ to a Fortran routine and vise versa. But at the
step
| of compilation I fail and don't know what to do at the moment:
| extern "C" void F77_FUNC (ffuncall, FFUNCALL) (int *vn, double *vd, void *vp);
Here you are claiming that funcall expects a void pointer, but
you are passing it something else:
| extern "C" void F77_FUNC (ffun, FFUN) (int *vint, double *vdbl) {
| printf (" Example 4a: C side: Fortran called C function ffon with numbers
%d %f\n",
| *vint, *vdbl);
| }
This declares a function with the type
void (*)(int*, double*)
and the compiler is telling you the types don't match.
You should declare ffuncall as
extern "C" void
F77_FUNC (ffuncall, FFUNCALL) (int *vn, double *vd, void (*)(int*, double*));
or better,
extern "C" F77_RET_T
F77_FUNC (ffuncall, FFUNCALL) (int *vn, double *vd, void (*)(int*, double*));
You might consider using const where appropriate.
You can also declare scalar Fortran parameters that are not modified
as const reference arguments to avoid needing to do things like this:
extern "C" F77_RET_T F77_FUNC (f, F) (int*);
...
int dummy = 42;
F77_FUNC (f, F) (&dummy);
and instead write
extern "C" F77_RET_T F77_FUNC (f, F) (const int&);
...
F77_FUNC (f, F) (42);
There are plenty of examples of this kind of thing in the Octave
sources.
jwe