[Next] [Previous] [Up] [Top] [Contents] [Index]
Chapter 16: Software Development
It is possible, with a little care, to combine C and FORTRAN modules in the same program. Some of the issues that need attention include:
For newly written C programs, you may wish to use the cfortran.h header file available in the cern product.
The July/Sept 1994 CERN Computer Newsletter (217) contains an extensive tutorial by Alfred Nathaniel on C and FORTRAN interfacing, available locally under the heading Software Development from the UNIX Resources Web page.
[Missing image]If you're programming under IRIX 6, you will need to choose an ABI. Refer to section 16.4.5.
We give here a summary of the techniques used on the Fermilab UNIX systems. In Appendix I you can find an example that illustrates much of the information in this section.
Generally, these variable types are equivalent:
FORTRAN | C |
|---|---|
INTEGER*1 | char |
INTEGER*2 | short |
INTEGER*4 | int |
REAL | float |
REAL*8 | double |
LOGICAL | (unavailable) |
C strings are zero-terminated, and have no intrinsic length. FORTRAN character variable lengths are given by an internal descriptor. FORTRAN character variables passed to C routines should be copied and zero-terminated before they are used.
The internal representation of FORTRAN LOGICAL variables is usually non-0/0 for .TRUE./.FALSE. respectively, but it is best not to count on this.
By default C starts indexes at 0 and FORTRAN starts them at 1. C and FORTRAN multiple index ordering is reversed. FORTRAN substring selection appears as the first C string index. See the following equivalence table:
FORTRAN | C |
|---|---|
| |
| |
| |
By default on Fermilab UNIX systems, the f77 compiler modifies FORTRAN subprogram and other external names. It forces each name to lower case, and appends an underscore. Thus FORTRAN label SUBPROG would become C label subprog_. For AIX, make sure the -qextname option is set (see section 16.4.2).
FORTRAN subprogram arguments are always passed as addresses (C pointers). C programs can specify arguments as either pointers or values. FORTRAN CHARACTER arguments are passed as pointers, followed by a set of additional values (not pointers) at the end of the argument list, giving the length of each CHARACTER argument.
C routines can always call FORTRAN routines, with due attention being given to arguments. FORTRAN routines cannot call arbitrary C routines.
FORTRAN COMMON's are accessible in C as extern structs, with the same name mapping as is used for entry points.
FORTRAN | C |
|---|---|
| |
| |
It is best to keep your FORTRAN COMMON variables aligned on natural boundaries[80], in order to avoid potential padding words which may be inserted differently by various FORTRAN and C compilers. You get natural alignment easily by putting longer variables before shorter variables in the COMMON.
Mixed C/FORTRAN I/O to the same file is not advisable. Mixed C/FORTRAN I/O to stdout, where stdout is the terminal, will usually work reasonably well, making debugging easier.
The easiest and safest way to link C/FORTRAN programs is to use the f77 command, which automatically includes both C and FORTRAN run time libraries. If you insist on linking with the cc or ld commands, remember to add the options:
-lF77 -lI77 -lm