calling c routines from fortran

Greg "Bucket" Woods woods at hao.UUCP
Tue Nov 20 04:24:18 AEST 1984

> Has anyone had experience with calling c routines from fortran 77 on 4.2
> unix? Know of any articles, pitfalls? 

   Unfortunately, due to the fact that F77 insists on appending a "_" to all
of it's routine names, you cannot call *any* C routine from F77 unless it's
name ends in "_". In addition to that, all F77 arguments are pointers, so you
can't even call a routine whose name ends in "_" if it is expecting any non-
pointer arguments (usually the case in C). Therefore, the only way to do this 
is the following (which is what I gather Liz was suggesting) (suppose you want
to call routine "z" which returns an int and has one int argument):


int iz_(intvar)  int *intvar;
int z(); /* this declaration not necessary for int functions */

  I know this is kludgy, but I use it all the time and it works fine. It's the
only thing you can do (well, maybe you could write the intermediate routine
in assembly, but I doubt if you could gain much efficiency that way, and it's
a lot more confusing).
   For a reference, see the paper "A Portable Fortran 77 Compiler" by S. I. 
Feldman and P.J. Weinberger, which, among other places, appears in the "Volume
2C" manual which came with our 4.2BSD manuals.

--Greg "FORTRAN hacker" Woods
{ucbvax!hplabs | allegra!nbires | decvax!stcvax | harpo!seismo | ihnp4!stcvax}
     "...once in a while you can get shown the light
         in the strangest of places if you look at it right..."

More information about the Comp.lang.c mailing list