www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/22/12:37:52

Date: Tue, 22 Jul 1997 12:36:24 -0400 (EDT)
From: "Art S. Kagel" <kagel AT ns1 DOT bloomberg DOT com>
To: "John M. Wildenthal" <j-wildenthal AT tamu DOT edu>
Cc: djgpp AT delorie DOT com
Subject: Re: Calling Fortran subroutines?
In-Reply-To: <33CFABEE.DAEBDD9D@tamu.edu>
Message-Id: <Pine.D-G.3.91.970722122150.10652C-100000@dg1>
Mime-Version: 1.0

On Fri, 18 Jul 1997, John M. Wildenthal wrote:

> I am trying to see if I must use F2C on Fortran77 code, or can I just
> compile it (using G77) and treat it as just another set of object code?
> 
> I have been told that C and Fortran pass arguments in different order
> (LIFO vs FIFO), but I was wondering if arrays were passed by address
> (like C) or value.

No, FORTRAN and C push arguments in the same order.  The difference is 
that C treats all arguments as pass by value while FORTRAN passes by 
reference.  Pass by reference is equivalent, in C, to always passing a 
pointer to every variable.  Also FORTRAN does not use NULL terminated 
strings and contains language level string support so any CHARACTER type 
argument also passes a hidden INTEGER*4 argument containing the length of 
the string.  Different FORTRANs pass this differently: some immediately 
following the associated string, some with all of the length parameters 
ganged up at the end of the argument list.  I don't know what GCC does.
It should be documented.

The answer to your latter question is that arrays are passed by address 
in FORTRAN like in C but so are all other argument types.  You can call C 
from FORTRAN and FORTRAN from C.  

FORTRAN usually shifts all names to lower case to and appends an 
underscore to global object names like subroutines and common blocks.  
Treat named common blocks as extern structures.  Just declare a 
struct/typedef to match the common's definition in FORTRAN.  Call all 
FORTRAN SUBROUTINES as C functions returning void with pointers for 
arguments in the same order as FORTRAN.

Similarly to access C from FORTRAN make sure that the function name ends 
in an underscore and that all arguments are pointers.  If the function 
returns a simple type you can call it as a FORTRAN function otherwise as 
a subroutine.  C global variables similarly must be named in lower case 
with the trailing underscore and can be declared in FORTRAN as a named 
common with the underscore stripped.

Art S. Kagel, kagel AT bloomberg DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019