home *** CD-ROM | disk | FTP | other *** search
- 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
- From: morgan@reg.triumf.ca (BURKE, MORGAN)
- Newsgroups: comp.lang.fortran
- Subject: Re: Fortran subroutine in C main (2)
- Message-ID: <20AUG199215211276@reg.triumf.ca>
- Date: 20 Aug 92 22:21:00 GMT
- References: <JAAKSI.92Aug18192406@korppi.cs.tut.fi>
- Sender: news@unixg.ubc.ca (Usenet News Maintenance)
- Distribution: comp.lang.fortran
- Organization: TRIUMF: Tri-University Meson Facility
- Lines: 73
- News-Software: VAX/VMS VNEWS 1.41
- Nntp-Posting-Host: reg.triumf.ca
-
- In article <JAAKSI.92Aug18192406@korppi.cs.tut.fi>, jaaksi@cs.tut.fi (Jaaksi Ari) writes...
- >How to use Fortran subroutines called from C main. Everything
- >else seems to be OK except when some IO -routines occur in
- >Fortran subroutines.
-
- Your problem is that f77 [fc] links in various support routines when you
- compile a Fortran main program, but not if you simply link Fortran
- objects to a C main program. The obvious solution is to use a Fortran
- main routine instead of C, and compile with f77 [fc].
-
- If you do not need use of argc and argv in your C main(), then this is
- easy:
-
- main.f:
- PROGRAM MAIN
- C all Fortran support code will be linked automatically
- CALL C_MAIN
- END
- main.c:
- void c_main_() /* trailing underscore required on some
- platforms; check your documentation */
- {
- /* this is your original C main() routine */
- }
-
- If you _do_ need to use argc and argv, then things are more complex.
- You must re-create argc and argv yourself from the Fortran environment.
- This is commonly done with the IARGC and GETARG calls (this is not
- standard, so check your docs). Then you simply call your original
- (but renamed) main() routine with the aritificial argc and argv.
- On an SGI, the following code works (thanks to Scott Townsend of
- Sverdrup Technology Inc. NASA Lewis Research Center Group):
-
- extern int iargc_ (void);
- extern void getarg_ (int *i, char *c, int c_len);
- extern int svp_main(int argc, char *argv[]);
-
- #define MAX_ARGS 20
- #define MAX_ARG_LENGTH 100
-
- int _argc;
- char *_argv[MAX_ARGS],
- _args[MAX_ARGS][MAX_ARG_LENGTH];
-
- void c_main_()
- {
- int i,j;
-
- strcpy(_args[0], "your_program_name");
- _argv[0] = _args[0];
- _argc = iargc_ () + 1;
- for (i = 1 ; i < _argc ; ++i) {
- getarg_ (&i, _args[i], MAX_ARG_LENGTH - 1);
- _argv[i] = _args[i];
- for (j = MAX_ARG_LENGTH - 2 ; j > 0 ; --j) {
- if (_args[i][j] == ' ')
- _args[i][j] = 0;
- else
- break;
- }
- }
- actual_c_main(_argc, _argv);
- }
-
- void actual_c_main(argc,argv)
- unsigned int argc;
- char **argv;
- {
- /* this is your original main() routine */
- }
-
- ................................................... Morgan Burke
- morgan@reg.triumf.ca
-