home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / fortran / 3100 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.8 KB  |  88 lines

  1. Path: sparky!uunet!convex!darwin.sura.net!mips!swrinde!elroy.jpl.nasa.gov!decwrl!bu.edu!rpi!uwm.edu!ogicse!zephyr.ens.tek.com!uw-beaver!ubc-cs!unixg.ubc.ca!reg.triumf.ca!morgan
  2. From: morgan@reg.triumf.ca (BURKE, MORGAN)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Fortran subroutine in C main (2)
  5. Message-ID: <20AUG199215211276@reg.triumf.ca>
  6. Date: 20 Aug 92 22:21:00 GMT
  7. References: <JAAKSI.92Aug18192406@korppi.cs.tut.fi>
  8. Sender: news@unixg.ubc.ca (Usenet News Maintenance)
  9. Distribution: comp.lang.fortran
  10. Organization: TRIUMF: Tri-University Meson Facility
  11. Lines: 73
  12. News-Software: VAX/VMS VNEWS 1.41
  13. Nntp-Posting-Host: reg.triumf.ca
  14.  
  15. In article <JAAKSI.92Aug18192406@korppi.cs.tut.fi>, jaaksi@cs.tut.fi (Jaaksi Ari) writes...
  16. >How to use Fortran subroutines called from C main. Everything
  17. >else seems to be OK except when some IO -routines occur in
  18. >Fortran subroutines.
  19.  
  20. Your problem is that f77 [fc] links in various support routines when you
  21. compile a Fortran main program, but not if you simply link Fortran 
  22. objects to a C main program.  The obvious solution is to use a Fortran 
  23. main routine instead of C, and compile with f77 [fc].
  24.  
  25. If you do not need use of argc and argv in your C main(), then this is 
  26. easy:
  27.  
  28. main.f:
  29.     PROGRAM MAIN
  30. C  all Fortran support code will be linked automatically
  31.     CALL C_MAIN
  32.     END
  33. main.c:
  34.     void c_main_()   /* trailing underscore required on some 
  35.                 platforms;  check your documentation */
  36.     {
  37.       /* this is your original C main() routine */
  38.     }
  39.  
  40. If you _do_ need to use argc and argv, then things are more complex.
  41. You must re-create argc and argv yourself from the Fortran environment.
  42. This is commonly done with the IARGC and GETARG calls (this is not
  43. standard, so check your docs).  Then you simply call your original
  44. (but renamed) main() routine with the aritificial argc and argv.
  45. On an SGI, the following code works (thanks to Scott Townsend of 
  46. Sverdrup Technology Inc.  NASA Lewis Research Center Group):
  47.  
  48. extern int  iargc_ (void);
  49. extern void getarg_ (int *i, char *c, int c_len);
  50. extern int  svp_main(int argc, char *argv[]);
  51.  
  52. #define MAX_ARGS        20
  53. #define MAX_ARG_LENGTH  100
  54.  
  55. int     _argc;
  56. char    *_argv[MAX_ARGS],
  57.         _args[MAX_ARGS][MAX_ARG_LENGTH];
  58.  
  59. void c_main_()
  60. {
  61.   int i,j;
  62.  
  63.   strcpy(_args[0], "your_program_name");
  64.   _argv[0] = _args[0];
  65.   _argc = iargc_ () + 1;
  66.   for (i = 1 ; i < _argc ; ++i) {
  67.     getarg_ (&i, _args[i], MAX_ARG_LENGTH - 1);
  68.     _argv[i] = _args[i];
  69.     for (j = MAX_ARG_LENGTH - 2 ; j > 0 ; --j) {
  70.       if (_args[i][j] == ' ')
  71.         _args[i][j] = 0;
  72.       else
  73.         break;
  74.     }
  75.   }
  76.   actual_c_main(_argc, _argv);
  77. }
  78.  
  79. void actual_c_main(argc,argv)
  80.     unsigned int argc;
  81.     char **argv;
  82. {
  83.   /* this is your original main() routine */
  84. }
  85.  
  86. ................................................... Morgan Burke
  87.                              morgan@reg.triumf.ca
  88.