home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20038 < prev    next >
Encoding:
Text File  |  1993-01-21  |  3.6 KB  |  113 lines

  1. Xref: sparky comp.lang.c:20038 comp.lang.fortran:5119
  2. Path: sparky!uunet!lhdsy1!kato.lahabra.chevron.com!hwrvo
  3. From: hwrvo@kato.lahabra.chevron.com (W.R. Volz)
  4. Newsgroups: comp.lang.c,comp.lang.fortran
  5. Subject: Re: calling a Fortran subroutine from a C program
  6. Message-ID: <9229@lhdsy1.lahabra.chevron.com>
  7. Date: 22 Jan 93 01:12:29 GMT
  8. References: <1jmqdaINN11g@mudhoney.mathcs.emory.edu>
  9. Sender: news@lhdsy1.lahabra.chevron.com
  10. Followup-To: comp.lang.c
  11. Organization: Chevron Oil Field Research Company
  12. Lines: 99
  13.  
  14. In article <1jmqdaINN11g@mudhoney.mathcs.emory.edu>, swhite@mathcs.emory.edu (Scott White) writes:
  15. |> Can someone please tell me how to call a Fortran subroutine from a C program?
  16. |> The environment is sun4, OS 4.1.2.
  17. |> 
  18. |> Also, the called Fortran routine takes variables which are arrays as 
  19. |> parameters.  And, I looked and saw that the subscripts start at 1, whereas in
  20. |> C they start at zero.  That's no problem is it (an address is an address):
  21. That is correct, it doesn't matter where the arrays start in fortran but
  22. in C they sill always start at 0.
  23. |> 
  24. |> /* .c file */
  25. |>     int num[100];
  26. |>     double zeta[100];
  27. |> 
  28. |>     foo(num, zeta);
  29. |> 
  30. |> 
  31. |> /* .f file */
  32. |>     subroutine foo(number, gamma)
  33. |>     integer*4 number(100)
  34. |>     real*8    gamma(100)
  35. |> 
  36. |>     /* program proceeds to manipulate values in the arrays, with subscripts
  37. |>         1 through 100, not 0 through 99 */
  38. |> 
  39. |> 
  40. |> Finally, if the Fortran subroutine changes the values in gamma, the changes
  41. |> show up when we return to the C program, right?
  42. Yes.
  43. |> 
  44. |> Your gracious help is deeply appreciated.
  45. |> 
  46. Some other things to think about: If you wanted to pass the number of
  47. elements to process:
  48.  
  49.  int n = 100;
  50.  foo(num,zeta,&n);
  51.  
  52.  subroutine foo(num,zeta,n)
  53.  ...
  54. Note that fortran always expects the address of something, C (strickly
  55. speaking always passes things by the value of the somethings. If you didn't
  56. include the &n in the c call to foo, the fortran foo would look for the
  57. number of elements at addess 100 and probably abort on the call. This
  58. is sometimes difficult to track down.
  59.  
  60. If you are passing character strings, all bets are off. It depends heavily
  61. on what fortran compiler you are using and how they handle character
  62. arrays. Sometimes the fortran arguement list is modified to include
  63. the length of the character string, so there may be extra arguments 
  64. sometwhere in the list. What I did is to write a short fortran routine
  65. that expects a byte or integer*1 array:
  66.  
  67.  subroutine c_str_to_for(cstr,fstr)
  68.  byte cstr(*)
  69.  character*(*) fstr
  70.  i = 1
  71. c set entire fortran string to blanks to handle shorter strings
  72.  fstr = ' '
  73. c don't overflow the fortran string and loop until the c null characte appears.
  74.  while(i .le. len(fstr).and.cstr(i) .ne. 0)
  75. c copy character and increment
  76.   fstr(i:i) = char(cstr(i))
  77.   i = i + 1
  78.  end do
  79. c or some such thing. This may not be completely correct, it's from memory.
  80.  return
  81.  
  82. and use this to convert c strings to fortran strings.
  83. so 
  84.  char *cstr = "This is a C string";
  85.  foo(str);
  86.  
  87.  subroutine foo(cstr)
  88.  byte cstr(*)
  89.  character*100 fstr
  90.  c_str_to_for(cstr,fsrt);
  91.  write(6,*) fstr
  92.  return
  93.  end
  94.  
  95. Also some compilers change the name of the fortran routine as seen
  96. by the linker. The MIPS compiler is notorius for this. It adds an
  97. underscore on the end of the name and changes all characters in the
  98. name to lower case. So on that compiler you'd do foo_(cstr); instead..
  99.  
  100. All in all, calling fortran from c and vice-versa can be a total bitch.
  101.  
  102. Have fun.
  103. s
  104.  
  105. -- 
  106.  
  107. ======================
  108. Bill Volz
  109. Chevron Petroleum Technology Co.
  110. Earth Model/Interpretation & Analysis Division.
  111. P.O. Box 446, La Habra, CA 90633-0446
  112. Phone: (310) 694-9340 Fax: (310) 694-7063
  113.