Go to the first, previous, next, last section, table of contents.


Startup Code

Unlike with some runtime systems, it shouldn't be necessary (unless there are bugs) to use a Fortran main program unit to ensure the runtime--specifically the I/O system--is initialized.

However, to use the g77 intrinsics GETARG and IARGC, either the main routine from the `libg2c' library must be used, or the f_setarg routine (new as of version 0.5.23 of g77) must be called with the appropriate argc and argv arguments prior to the program calling GETARG or IARGC.

To provide more flexibility for mixed-language programming involving g77 while allowing for shared libraries, as of version 0.5.23, g77's main routine in libg2c does the following, in order:

  1. Calls f_setarg with the incoming argc and argv arguments, in the same order as for main itself. This sets up the command-line environment for GETARG and IARGC.
  2. Calls f_setsig (with no arguments). This sets up the signaling and exception environment.
  3. Calls f_init (with no arguments). This initializes the I/O environment, though that should not be necessary, as all I/O functions in libf2c are believed to call f_init automatically, if necessary. (A future version of g77 might skip this explicit step, to speed up normal exit of a program.)
  4. Arranges for f_exit to be called (with no arguments) when the program exits. This ensures that the I/O environment is properly shut down before the program exits normally. Otherwise, output buffers might not be fully flushed, scratch files might not be deleted, and so on. The simple way main does this is to call f_exit itself after calling MAIN__ (in the next step). However, this does not catch the cases where the program might call exit directly, instead of using the EXIT intrinsic (implemented as exit_ in libf2c). So, main attempts to use the operating environment's onexit or atexit facility, if available, to cause f_exit to be called automatically upon any invocation of exit.
  5. Calls MAIN__ (with no arguments). This starts executing the Fortran main program unit for the application. (Both g77 and f2c currently compile a main program unit so that its global name is MAIN__.)
  6. If no onexit or atexit is provided by the system, calls f_exit.
  7. Calls exit with a zero argument, to signal a successful program termination.
  8. Returns a zero value to the caller, to signal a successful program termination, in case exit doesn't exit on the system.

All of the above names are C extern names, i.e. not mangled.

When using the main procedure provided by g77 without a Fortran main program unit, you need to provide MAIN__ as the entry point for your C code. (Make sure you link the object file that defines that entry point with the rest of your program.)

To provide your own main procedure in place of g77's, make sure you specify the object file defining that procedure before `-lg2c' on the g77 command line. Since the `-lg2c' option is implicitly provided, this is usually straightforward. (Use the `--verbose' option to see how and where g77 implicitly adds `-lg2c' in a command line that will link the program. Feel free to specify `-lg2c' explicitly, as appropriate.)

However, when providing your own main, make sure you perform the appropriate tasks in the appropriate order. For example, if your main does not call f_setarg, make sure the rest of your application does not call GETARG or IARGC.

And, if your main fails to ensure that f_exit is called upon program exit, some files might end up incompletely written, some scratch files might be left lying around, and some existing files being written might be left with old data not properly truncated at the end.

Note that, generally, the g77 operating environment does not depend on a procedure named MAIN__ actually being called prior to any other g77-compiled code. That is, MAIN__ does not, itself, set up any important operating-environment characteristics upon which other code might depend. This might change in future versions of g77, with appropriate notification in the release notes.

For more information, consult the source code for the above routines. These are in `gcc/f/runtime/libF77/', named `main.c', `setarg.c', `setsig.c', `getarg_.c', and `iargc_.c'.

Also, the file `gcc/f/com.c' contains the code g77 uses to open-code (inline) references to IARGC.


Go to the first, previous, next, last section, table of contents.