C subroutine calls from FORTRAN

tps at sdchem.UUCP tps at sdchem.UUCP
Thu Feb 19 18:25:19 AEST 1987


In article <2324 at usceast.UUCP> still at usceast.UUCP (Bert Still) writes:
>When I try to compile and link the following routine (and a few others
>which are insignificant for this particular query), I get a bunch of
>unresolved external reference messages from the linker. This is apparently
>because when you call a routine from f77, an underscore (_) is appended to
>the fcn identifier both in front (as cc does) and BEHIND. How does one get
>around this problem ? Any ideas would be greatly appreciated.

The answer is simple.  You can't call C routines directly from fortran unless
the latter end in an underscore.  THIS IS DEFINITELY A FEATURE.  Fortran calls
by address, C by value, so 7 times out of 8 you wouldn't want to call the
C routine directly.  The answer is, for each C routine you want to call, to
write a front-end routine in C which the fortran code actually calls, which
ends in underscore, and which properly translates to call-by-value.  For
instance, isatty() has the synopsis

	isatty( fd )
		int	fd;

Write a front end which (in the C code) you call isatty_():

	isatty_( fd )
		int	*fd;	/* a POINTER, which is what fortran passes */
	{
		return	isatty( *fd );
	}

Now in your fortran code you just do
	
	integer function isatty()
	integer ifd, ttyinput

	ifd =	0
	ttyinput =	isatty(ifd)

|| Tom Stockfisch, UCSD Chemistry	tps%chem at sdcsvax.UCSD



More information about the Comp.unix.questions mailing list