[comp.lang.c] Calling C routines from Fortran programs.

dik@cwi.nl (Dik T. Winter) (05/25/88)

I know how to call C routines from Fortran programs on the systems
I work on.  What I want to know is if there are other systems that
require other methods.

The facts. Given a C routine:
	foo(a, n) int *a, n;
that I want to call from a Fortran program as
	K = BAR(A, N)
What should the interface routine (written in C) look like?
The general Unix solution is:
	bar_(a, n) int *a, *n;
	{
		return(foo(a, *n));
	}
Another interface I have encountered is:
	BAR(a, n) int *a, *n;
	{
		return(foo(a, *n));
	}
Reading about the cdecl keyword on the net, I suspect tha Microsoft
interface ought to be:
	cdecl int foo();
	fortran int bar(a, n) int *a, *n;
	{
		return(foo(a, *n));
	}
(but correct me if I am wrong).
And I know one system where I have to write:
	BAR(a, n) int a, n;
	{
		return(foo((int *)(a >> 16), *((int *)(n >> 16))));
	}
because addresses in Fortran and pointers in C are not aligned the same way.

If you know other methods please mail me, I will summarise on the net.
-- 
dik t. winter, cwi, amsterdam, nederland
INTERNET   : dik@cwi.nl
BITNET/EARN: dik@mcvax

shankar@hpclscu.HP.COM (Shankar Unni) (05/25/88)

> / hpclscu:comp.lang.c / dik@cwi.nl (Dik T. Winter) / 12:55 pm  May 24, 1988 /
> I know how to call C routines from Fortran programs on the systems
> I work on.  What I want to know is if there are other systems that
> require other methods.
> 
  [Lots of ugly kludges deleted...]

Is there any more persuasive argument for a coherent procedure calling and
naming convention across all compilers on any given architecture?

Apart from the odd stumbling block (like FORTRAN always insisting on passing
parms by ref), writing code in multiple languages can be made sooo easy.

---
shankar

henry@utzoo.uucp (Henry Spencer) (05/29/88)

> Is there any more persuasive argument for a coherent procedure calling and
> naming convention across all compilers on any given architecture?

It's been tried.  Unfortunately, it has a tendency to be somewhat of a
Rube Goldberg affair, with the result that individual compilers implement
only the parts actually used by their languages.  This isn't much of an
improvement.  There are also fundamental problems of data incompatibility,
such as the widely-varying definitions of strings.
-- 
"For perfect safety... sit on a fence|  Henry Spencer @ U of Toronto Zoology
and watch the birds." --Wilbur Wright| {ihnp4,decvax,uunet!mnetor}!utzoo!henry