[PD] Fortran-cross-C usage

Charles Henry czhenry at gmail.com
Mon Mar 20 19:27:47 CET 2006


Hi, list,
  I've run into a certain problem with accessing a Fortran subroutine
from C.  I thouhgt that I could just look up the symbols and make some
header for the C function which calls the Fortran subroutine.
  My Fortran sub is called SolveT, and is found in a module called
Toeplitz_Solver.  I looked up the symbol in the compiled binary; it's
toeplitz_solver_mp_solvet_

The makefile compiles the two things separately:
$(F90) -shared -o libsft.a $(FFLAGS) Toeplitz_Solver.o verbose.o
timing.o ratintnf.o precision.o $(DFFT)

so I compile my Fortran code together, (using the intel fortran
compiler) into a library that I'm calling libsft.a
Similarly, I put the C code together in another object:
gcc $(LINUXINCLUDE) $(LINUXCFLAGS) -o controlfreak~.o -c controlfreak~.c

and link them:
ld -export-dynamic -shared -o controlfreak~.pd_linux ...
controlfreak~.o -lsft -lfftw3f -lc

controlfreak~.c has the following header for using this:

extern void toeplitz_solver_mp_solvet_(integer * , double *, double *,
integer *, double *);

I figured since Fortran passes by reference, I would need to define
all my variables as pointers.  The usage, looks like this,

toeplitz_solver_mp_solvet_(&tempn,XT,m,&itref,&factorref);

where XT and m are arrays, tempn, itref, and factorref are parameters.
 m is also the return array for the operation.  I've checked it out
with valgrind, and the error which pops up, before seg fault, is

==3735== Invalid read of size 8
==3735==    at 0x1C583F6D: toeplitz_solver_mp_solvet_ (in
/usr/local/lib/pd/extra/controlfreak~/libsft.a)
==3735==    by 0x1B91995F: controlfreak_tilde_perform (in
/usr/local/lib/pd/extra/controlfreak~/controlfreak~.pd_linux)
==3735==    by 0x80A5859: dsp_tick (d_ugen.c:304)
==3735==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

==3735== Process terminating with default action of signal 11 (SIGSEGV)
==3735==  Access not within mapped region at address 0x0
==3735==    at 0x1C583F6D: toeplitz_solver_mp_solvet_ (in
/usr/local/lib/pd/extra/controlfreak~/libsft.a)
==3735==    by 0x1B91995F: controlfreak_tilde_perform (in
/usr/local/lib/pd/extra/controlfreak~/controlfreak~.pd_linux)
==3735==    by 0x80A5859: dsp_tick (d_ugen.c:304)

So, I think I've got the right form for the function prototype.  It
apparently calls solvet, but I wonder what's going wrong to put that
Adress 0x0 in there.

Chuck




More information about the Pd-list mailing list