From: marek@iiasa.ac.at (Marek  MAKOWSKI)
Subject: Re: FORTRAN/C Interface: C<=>FORTRAN data transfer?
Organization: IIASA, Laxenburg, Austria
X-Newsreader: TIN [version 1.1 PL6]
Date: Fri, 20 Nov 1992 17:36:16 GMT

John H. Chauvin (jchauvin@netcom.com) wrote:
: 
: My current thinking is that the C program would call the FORTRAN program and
: pass the filenames somehow.
: 
: Other Questions:
: 
: (1) I am not sure whether to call the FORTRAN program from my C 
: program or the other way around? Any suggestions?
: 
: (2) How do I handle the fact that both programs have a main routine?
: 
: (3) If I transfer data from C to FORTRAN using a C data structure to 
: COMMON, does the C data structure have to be global?
: 
: Thanks for the help! I need it.
: 
: jchauvin@netcom.com
: 
: -- 
: John H. Chauvin jchauvin@netcom.COM
: Netcom - Online Communication Services San Jose, CA
I have positive experience with using C & fortran on both Sun and on PC.
It is pretty easy to do, even if you want the code to be portable between
Unix and DOS.
The most complete discussion about various details I have seen in the
Microsoft Fortran manual.

Short answers to your questions:
1. You can call fortran from C and vice versa
2. You should have only one main() (either in C or in fortran)
3. You can transfer data between C data structure and labeled fortran common

Below please find short exmpale of calling fortran function from C
(it is concatenation of 3 files (*.c *.f and Makefile).
As you see from this example (which is for Unix only) the names
differ between fortran and C by a _ and fortran does not know how to
get a parameter by value.
Hope this example (which was originally provided by Jurek Sobczyk)
is informative enough.

Good luck,
Marek



/**********************************************************************/
/* a.c            v 1.0     by J.Sobczyk                   1991.06.19 */
/*                                                                    */
/* c to f77 linking example                                           */
/**********************************************************************/
#include <stdio.h>

extern double x_();

void main( )
  {
  double r = 2.0, *rp;
  int i = 3;

  printf( "Hello\n" );
  r = x_( &i, &r );
  printf( "%lf\n", r );
  printf( "Nice to see you again\n" );
  }

/**********************************************************************/

C**********************************************************************C
C* x.f            v 1.0     by J.Sobczyk                   1991.06.19 *C
C*                                                                    *C
C* C++ to f77 linking example.                                        *C
C**********************************************************************C

	DOUBLEPRECISION FUNCTION X( I, R )
	INTEGER I
	DOUBLEPRECISION R

    	X = R * I
	END

C**********************************************************************C

#**********************************************************************#
#* Makefile       v 1.0     by J.Sobczyk                   1991.07.12 *#
#*                                                                    *#
#* c to f77 linking example                                           *#
#**********************************************************************#

all: a.o x.o
	f77 a.o x.o -o a

.c.o:
	cc -g -c $*.c -o $*.o

.f.o:
	f77 -g -c $*.f -o $*.o

clean:
	rm a.o x.o a

#**********************************************************************#

========================================================================
| Jerzy Sobczyk            Institute of Automatic  Control  /##/ /#####|
| J.Sobczyk@ia.pw.edu.pl   Warsaw University of Technology /##/ /##/|##|
| tlx. 813 307 pw pl              Nowowiejska 15/19       /##/ /##/ |##|
| fax. (+48)(22) 25 37 19           00-565 Warsaw,       /##/ /####>|##|
| tel. (+48)(22) 21 00 7 ext. 864       POLAND          /##/ /##/   |##|
========================================================================


-- 
                  Marek Makowski  | Email: marek@iiasa.ac.at
         International Institute  | Phone: +43 02236 71.5210
     for Applied System Analysis  | Fax:   +43 02236 71.313
       A-2361 Laxenburg, Austria  | Telex: 079137 iiasa a
Newsgroups: comp.lang.fortran