Node: Main Program Unit, Next: , Up: Debugging and Interfacing



Main Program Unit (PROGRAM)

When g77 compiles a main program unit, it gives it the public procedure name MAIN__. The libg2c library has the actual main() procedure as is typical of C-based environments, and it is this procedure that performs some initial start-up activity and then calls MAIN__.

Generally, g77 and libg2c are designed so that you need not include a main program unit written in Fortran in your program--it can be written in C or some other language. Especially for I/O handling, this is the case, although g77 version 0.5.16 includes a bug fix for libg2c that solved a problem with using the OPEN statement as the first Fortran I/O activity in a program without a Fortran main program unit.

However, if you don't intend to use g77 (or f2c) to compile your main program unit--that is, if you intend to compile a main() procedure using some other language--you should carefully examine the code for main() in libg2c, found in the source file gcc/libf2c/libF77/main.c, to see what kinds of things might need to be done by your main() in order to provide the Fortran environment your Fortran code is expecting.

For example, libg2c's main() sets up the information used by the IARGC and GETARG intrinsics. Bypassing libg2c's main() without providing a substitute for this activity would mean that invoking IARGC and GETARG would produce undefined results.

When debugging, one implication of the fact that main(), which is the place where the debugged program "starts" from the debugger's point of view, is in libg2c is that you won't be starting your Fortran program at a point you recognize as your Fortran code.

The standard way to get around this problem is to set a break point (a one-time, or temporary, break point will do) at the entrance to MAIN__, and then run the program. A convenient way to do so is to add the gdb command

     tbreak MAIN__
     

to the file .gdbinit in the directory in which you're debugging (using gdb).

After doing this, the debugger will see the current execution point of the program as at the beginning of the main program unit of your program.

Of course, if you really want to set a break point at some other place in your program and just start the program running, without first breaking at MAIN__, that should work fine.