|
|
BSD 4.3tahoe
.TI F77/LINK_TO_C "Sep. 4, 1985"
Linking to C Procedures
C is a commonly used programming language in UNIX. "A Portable
Fortran 77 Compiler" gives complete specification of the C/Fortran
interface. This help file summarizes the basics for simple cases and
gives a simple example.
The following table from that paper lists corresponding Fortran and C
declarations:
.nf
Fortran C
----------- ---------
integer*2 x short int x;
integer x long int x;
logical x long int x;
real x float x;
double precision x double x;
complex x struct { float r, i; } x;
double complex x struct { double dr, di; } x;
character*6 x char x[6];
.fi
The following Fortran statements calls a procedure fill() that
sets the first 10 elements of ix() to the value 20:
.nf
integer ix(100)
c
call fill(ix, 10, 20)
.fi
In creating external symbols for subprogram and common block names,
f77 adds an underscore before and after external names,
while the C compiler only adds an underscore before the name.
Thus the C name fill_() corresponds to the f77 name fill();
and the corresponding C procedure is:
.nf
fill_( vec, nvec, val)
long *vec, *nvec, *val;
{
long nv = *nvec;
while (nv-- > 0) *vec++ = *val;
}
.fi
Since Fortran passes variables by address, the C procedure treats the
parameters as pointers. This also means that a temporary must be used.
If a temporary is not used, as in:
while ((*nvec)-- > 0) *vec++ = *val;
the value of the third parameter in the call would be destroyed.
When mixing C and Fortran, if I/O is done in both languages and
the main program is in C, then call 'f_init()' in the
Fortran I/O library to initialize units 0, 5, and 6 to standard error,
standard input and standard output and to line-buffer standard error.
C stores arrays in row-major order with subscripts starting at 0
while Fortran stores arrays in column-major order with subscripts
starting (by default) at 1.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.