linking c and f77 programs [and pascal programs]

chris at umcp-cs.UUCP chris at umcp-cs.UUCP
Thu Apr 5 17:02:43 AEST 1984


In *.*BSD (and probably therefore System *), if you want to
call a C subroutine from F77 code, write the C subroutine normally,
but change parameter types:

	f77 type		C type
	--------		------
	integer			int *
	integer*2		short *
	real*4			float *
	real*8			double *
	complex*8		struct complex *
	complex*16		struct dcomplex *

				struct complex { float re, im; };
				struct dcomplex { double re, im; };

Also, if the f77 routine wants to call "foo", name the C routine
"foo_".

Return values should just be return'ed (with the appropriate type)
in the case of integer and real*8.  I'm not sure about the others.

Example:

	program test
	integer i, j
	integer function cfunc

	do 10 i = 1, 100
		j = cfunc (i)
10	continue

	end

int
cfunc_ (i)
int *i;
{
	<whatever>;
	return retval;
}

--------

To call Fortran routines from Pascal is considerably more difficult,
because Pascal won't let you use "_" in identifiers.  (#@%^*$&)  One
method is to use a C interface routine:

to call f77 function "func" from pascal:

	integer function func (i, j, k)
	integer i, j, k
	...
	return
	end

int
xfunc (i, j, k)		/* x-ecute f77 "func" */
int *i, *j, *k;
{
	return func_ (i, j, k);
}

{ I'm rather rusty on Pascal; ignore minor syntactic mistakes }
function xfunc (var i, j, k : integer) : integer external;
program prog (input, output);
begin
	...
	xfunc (...);
	...
end.

Note also that if you don't want the hassle of providing "var" parameters,
you can have your C code written like this:

int
xfunc (i, j, k)
int i, j, k;
{
	return func_ (&i, &j, &k);
}

This implies that even if the Fortran function modifies its parameters,
it can't affect the Pascal values.

Chris
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris.umcp-cs at CSNet-Relay



More information about the Comp.lang.c mailing list