|
|
1.1 root 1: .TI F77/LINK_TO_C "Sep. 4, 1985"
2: Linking to C Procedures
3:
4: C is a commonly used programming language in UNIX. "A Portable
5: Fortran 77 Compiler" gives complete specification of the C/Fortran
6: interface. This help file summarizes the basics for simple cases and
7: gives a simple example.
8:
9: The following table from that paper lists corresponding Fortran and C
10: declarations:
11:
12: .nf
13: Fortran C
14: ----------- ---------
15: integer*2 x short int x;
16: integer x long int x;
17: logical x long int x;
18: real x float x;
19: double precision x double x;
20: complex x struct { float r, i; } x;
21: double complex x struct { double dr, di; } x;
22: character*6 x char x[6];
23: .fi
24:
25:
26: The following Fortran statements calls a procedure fill() that
27: sets the first 10 elements of ix() to the value 20:
28:
29: .nf
30: integer ix(100)
31: c
32: call fill(ix, 10, 20)
33: .fi
34:
35: In creating external symbols for subprogram and common block names,
36: f77 adds an underscore before and after external names,
37: while the C compiler only adds an underscore before the name.
38: Thus the C name fill_() corresponds to the f77 name fill();
39: and the corresponding C procedure is:
40:
41: .nf
42: fill_( vec, nvec, val)
43: long *vec, *nvec, *val;
44: {
45: long nv = *nvec;
46:
47: while (nv-- > 0) *vec++ = *val;
48: }
49: .fi
50:
51: Since Fortran passes variables by address, the C procedure treats the
52: parameters as pointers. This also means that a temporary must be used.
53: If a temporary is not used, as in:
54:
55: while ((*nvec)-- > 0) *vec++ = *val;
56:
57: the value of the third parameter in the call would be destroyed.
58:
59: When mixing C and Fortran, if I/O is done in both languages and
60: the main program is in C, then call 'f_init()' in the
61: Fortran I/O library to initialize units 0, 5, and 6 to standard error,
62: standard input and standard output and to line-buffer standard error.
63:
64: C stores arrays in row-major order with subscripts starting at 0
65: while Fortran stores arrays in column-major order with subscripts
66: starting (by default) at 1.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.