Next: C++ Considerations, Previous: C Access to Type Information, Up: Interoperating with C and C++
A simple and foolproof way to write g77-callable C routines—e.g. to
interface with an existing library—is to write a file (named, for
example, fred.f) of dummy Fortran
skeletons comprising just the declaration of the routine(s) and dummy
arguments plus END
statements.
Then run f2c on file fred.f to produce fred.c
into which you can edit
useful code, confident the calling sequence is correct, at least.
(There are some errors otherwise commonly made in generating C
interfaces with f2c conventions,
such as not using doublereal
as the return type of a REAL
FUNCTION
.)
f2c also can help with calling Fortran from C, using its
-P option to generate C prototypes appropriate for calling the
Fortran.1
If the Fortran code containing any
routines to be called from C is in file joe.f, use the command
f2c -P joe.f to generate the file joe.P containing
prototype information.
#include
this in the C which has to call
the Fortran routines to make sure you get it right.
See Arrays (DIMENSION), for information on the differences between the way Fortran (including compilers like g77) and C handle arrays.
[1] The files generated like this can also be used for inter-unit consistency checking of dummy and actual arguments, although the ftnchek tool from ftp://ftp.netlib.org/fortran or ftp://ftp.dsm.fordham.edu is probably better for this purpose.