home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / fortran / 4378 < prev    next >
Encoding:
Text File  |  1992-11-20  |  4.3 KB  |  129 lines

  1. Newsgroups: comp.lang.fortran
  2. Path: sparky!uunet!news.univie.ac.at!iiasa.ac.at!marek
  3. From: marek@iiasa.ac.at (Marek  MAKOWSKI)
  4. Subject: Re: FORTRAN/C Interface: C<=>FORTRAN data transfer?
  5. Message-ID: <1992Nov20.173616.6697@iiasa.ac.at>
  6. Organization: IIASA, Laxenburg, Austria
  7. X-Newsreader: TIN [version 1.1 PL6]
  8. References: <1992Nov15.012720.3466@netcom.com>
  9. Date: Fri, 20 Nov 1992 17:36:16 GMT
  10. Lines: 117
  11.  
  12. John H. Chauvin (jchauvin@netcom.com) wrote:
  13. : My current thinking is that the C program would call the FORTRAN program and
  14. : pass the filenames somehow.
  15. : Other Questions:
  16. : (1) I am not sure whether to call the FORTRAN program from my C 
  17. : program or the other way around? Any suggestions?
  18. : (2) How do I handle the fact that both programs have a main routine?
  19. : (3) If I transfer data from C to FORTRAN using a C data structure to 
  20. : COMMON, does the C data structure have to be global?
  21. : Thanks for the help! I need it.
  22. : jchauvin@netcom.com
  23. : -- 
  24. : John H. Chauvin jchauvin@netcom.COM
  25. : Netcom - Online Communication Services San Jose, CA
  26. I have positive experience with using C & fortran on both Sun and on PC.
  27. It is pretty easy to do, even if you want the code to be portable between
  28. Unix and DOS.
  29. The most complete discussion about various details I have seen in the
  30. Microsoft Fortran manual.
  31.  
  32. Short answers to your questions:
  33. 1. You can call fortran from C and vice versa
  34. 2. You should have only one main() (either in C or in fortran)
  35. 3. You can transfer data between C data structure and labeled fortran common
  36.  
  37. Below please find short exmpale of calling fortran function from C
  38. (it is concatenation of 3 files (*.c *.f and Makefile).
  39. As you see from this example (which is for Unix only) the names
  40. differ between fortran and C by a _ and fortran does not know how to
  41. get a parameter by value.
  42. Hope this example (which was originally provided by Jurek Sobczyk)
  43. is informative enough.
  44.  
  45. Good luck,
  46. Marek
  47.  
  48.  
  49.  
  50. /**********************************************************************/
  51. /* a.c            v 1.0     by J.Sobczyk                   1991.06.19 */
  52. /*                                                                    */
  53. /* c to f77 linking example                                           */
  54. /**********************************************************************/
  55. #include <stdio.h>
  56.  
  57. extern double x_();
  58.  
  59. void main( )
  60.   {
  61.   double r = 2.0, *rp;
  62.   int i = 3;
  63.  
  64.   printf( "Hello\n" );
  65.   r = x_( &i, &r );
  66.   printf( "%lf\n", r );
  67.   printf( "Nice to see you again\n" );
  68.   }
  69.  
  70. /**********************************************************************/
  71.  
  72. C**********************************************************************C
  73. C* x.f            v 1.0     by J.Sobczyk                   1991.06.19 *C
  74. C*                                                                    *C
  75. C* C++ to f77 linking example.                                        *C
  76. C**********************************************************************C
  77.  
  78.     DOUBLEPRECISION FUNCTION X( I, R )
  79.     INTEGER I
  80.     DOUBLEPRECISION R
  81.  
  82.         X = R * I
  83.     END
  84.  
  85. C**********************************************************************C
  86.  
  87. #**********************************************************************#
  88. #* Makefile       v 1.0     by J.Sobczyk                   1991.07.12 *#
  89. #*                                                                    *#
  90. #* c to f77 linking example                                           *#
  91. #**********************************************************************#
  92.  
  93. all: a.o x.o
  94.     f77 a.o x.o -o a
  95.  
  96. .c.o:
  97.     cc -g -c $*.c -o $*.o
  98.  
  99. .f.o:
  100.     f77 -g -c $*.f -o $*.o
  101.  
  102. clean:
  103.     rm a.o x.o a
  104.  
  105. #**********************************************************************#
  106.  
  107. ========================================================================
  108. | Jerzy Sobczyk            Institute of Automatic  Control  /##/ /#####|
  109. | J.Sobczyk@ia.pw.edu.pl   Warsaw University of Technology /##/ /##/|##|
  110. | tlx. 813 307 pw pl              Nowowiejska 15/19       /##/ /##/ |##|
  111. | fax. (+48)(22) 25 37 19           00-565 Warsaw,       /##/ /####>|##|
  112. | tel. (+48)(22) 21 00 7 ext. 864       POLAND          /##/ /##/   |##|
  113. ========================================================================
  114.  
  115.  
  116. -- 
  117.                   Marek Makowski  | Email: marek@iiasa.ac.at
  118.          International Institute  | Phone: +43 02236 71.5210
  119.      for Applied System Analysis  | Fax:   +43 02236 71.313
  120.        A-2361 Laxenburg, Austria  | Telex: 079137 iiasa a
  121.