calling c routines from fortran
Liz Sonenberg
lizs at munnari.OZ
Sat Nov 24 10:09:46 AEST 1984
> 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)
>[ details of proposed method comes here ]
Actually there is a rather horrible kludge for passing an immediate value
(rather than a pointer) directly from an f77 program to a c procedure.
Suppose you want to call the c procedure whose synopsis is
int z_(intvar) int intvar;
The following f77 code will pass the value of the integer quantity i to the
procedure z_ as an immediate value
...
character*1 x(2)
...
iretval = z( x( i + 1 - loc(x) )
...
(provided your machine is byte-addressable, and each character occupies a
single byte). Naturally, you would have to compile the resulting program
without the -C option so that no run-time checking of array subscripts will
be performed.
Getting access to immediate values that are passed back the other way is a
different story, however, and I know of no convenient way of doing it.
It is also possible to circumvent the compiler's (very irritating) procedure
of appending underscores to all user-established global variables, provided
the name of the c routine you want to call contains at least two characters.
When calling the routine from your f77 program, give it any convenient name
that contains one less character. After compiling (but not linking) the f77
progam, the temporary name you have given the routine will appear in the string
table of the object file with an underscore appended. It is not particularly
difficult to replace this with the true name of the routine you want to call
( I have a rudimentary c program for doing this, if anyone is interested ).
After the modified object file has been linked with the appropriate c files or
libraries, your f77 program will then call the correct routine.
David Wilson
{ ...!decvax!munnari!lizs }
More information about the Comp.lang.c
mailing list