Date: Fri, 9 May 1997 11:45:56 -0400 (EDT) From: "Art S. Kagel" To: Dobler Ervin Cc: djgpp AT delorie DOT com Subject: Re: Calling Fortran code from a C program In-Reply-To: <337312EF.2B7@hercules.elte.hu> Message-Id: Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Fri, 9 May 1997, Dobler Ervin wrote: > I have a package of routines written in Fortran77, and I would like to > call them from a C program. I'm using GNU's g77 and GNU C. > If there is somebody who can answer my questions I would be very > grateful. I have not used g77 specifically but have written mixed C & FORTRAN programs in several environments. > My questions are: > 1. How can I specify the appropriate calling convention? You need not, "C" and FORTRAN use essentially the same calling conventions. The languages are easily mixed noting the items below: > 2. What type of name mangling does the f77 compiler use? FORTRAN shifts all identifiers to lower case and prepends an underscore (_) character. IE the FORTRAN function ReadMyFile() is callable as the "C" function _readmyfile() and the FORTRAN COMMON block /MyData/ is referenced in "C" as the Global variable _mydata which you can declare in "C" as a struct matching the structure of the elements (or equivalenced types) of the common. > 3. Should I have indicate something in the fortran code as well? No. > 4. What are the appropriate gcc command-line switches for .f and .c > files? > 5. I would like to access variables, arrays in the fortran code as well. > Does it differs from accessing functions? To call FORTRAN from "C" just remember that all arguments are pointers and that an additional hidden argument, type int (not int*), is supplied by FORTRAN for each character variable containing the length of the string. Some FORTRANs insert the hidden argument immediately following the associated character argument, but most gather them all at the end of the argument list in order corresponding to the strings that they describe. Obviously, FORTRAN functions return something, subroutines do not. Also remember that FORTRAN deals with fixed length character strings which are blank padded at the end and DO NOT TERMINATE WITH A NULL character except by accident so you must write your own "C" functions to deal with this type of character string. > > Please send your answers at 'dobler AT hercules DOT elte DOT hu'. > Thanks in advance > > Ervin Dobler > Technical University of Budapest, Department of Atomic Physics. > Art S. Kagel, kagel AT bloomberg DOT com