Calling Fortran from C (Summary)

Subbarao Jagannatha jagannat at caip.rutgers.edu
Fri Feb 8 10:58:26 AEST 1991


This is the summary of my experiences in calling Fortran routines
from C main program in SUN OS 4.03.

1. I was getting the run-time error of segmentation violation
   at every WRITE statements in Fortran. There was no warning
   or error messages while compiling or linking. Initially I thought 
   it could be Fortran I/O initialization problem. But, the source
   of the problem was found to be the name of a function in C (scale),
   which clashed with a Fortran library subroutine by the same 
   name. There is a long list of Fortran library subroutine names.
   The credit for debugging should go to Mr. Jim Wade of SUN.

2. The SUN-3 debugger is good.  SUN-4(SPARC) gives problems 
   while using debugger.

Few more points about calling Fortran from C:

1. If the name of Fortran subroutine called from C main is "test",
   then use the statement "test_();" in C main for calling it.
   Here no arguments are passed to Fortran.

2. If you wish to pass large number of arguments to Fortran,
   the easier way could be to use "common" statement in Fortran.

   For example, to pass the following structure in C :
        struct object_data {
            float surface[50][12];
	    int   bound[50][50];
	    float intersect[80][8];
            int   vertex[80][15];
          } dbs_ ;
   Note: the underscore after the variable name dbs is required.
   Use the following statements at the begining of Fortran program:
     integer bound(50,50), vertex(15,80)
     real*4 surface(12,50), intersect(8,80)
     common /dbs/surface,bound,intersect,vertex

 3. Do not use any integer*2 declaration in Fortran for any common
    arguments. Also do not use any "short int" declarations in C,
    for any common arguments. I am aware of only these two, but there
    may be some more restrictions.

 4. For compiling use the "cc" command for both C and Fortran programs.
    The programs should be linked to Fortran and C library using options:
          -lF77 -lU77 -lI77 -lc -lm

 5. Call the routines "f_init();" at the begining of C main to
    initialize Fortran I/O , and "f_exit();" at the end of C main.

   I  wish to acknowledge the help of Mr. Keith Bierman of SUN.
Also many other people gave me suggestions for which I am grateful.


Jagan
jagannat at caip.rutgers.edu
Rutgers University, New Jersey.



More information about the Comp.lang.c mailing list