Node: Startup Code, Previous: C++ Considerations, Up: Interoperating with C and C++
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
main routine from the
libg2c library must be used,
(new as of
egcs version 1.1 and
g77 version 0.5.23)
must be called with the appropriate
prior to the program calling
To provide more flexibility for mixed-language programming
g77 while allowing for shared libraries,
egcs version 1.1 and
g77 version 0.5.23,
main routine in
does the following, in order:
f_setargwith the incoming
argvarguments, in the same order as for
This sets up the command-line environment
f_setsig(with no arguments).
This sets up the signaling and exception environment.
f_init(with no arguments).
This initializes the I/O environment,
though that should not be necessary,
as all I/O functions in
are believed to call
(A future version of
g77 might skip this explicit step,
to speed up normal exit of a program.)
f_exitto 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
f_exit itself after calling
MAIN__ (in the next step).
However, this does not catch the cases where the program
instead of using the
main attempts to use
the operating environment's
facility, if available,
f_exit to be called automatically
upon any invocation of
MAIN__(with no arguments).
This starts executing the Fortran main program unit for
f2c currently compile a main
program unit so that its global name is
atexitis provided by the system, calls
exitwith a zero argument, to signal a successful program termination.
exitdoesn't exit on the system.
All of the above names are C
i.e. not mangled.
When using the
main procedure provided by
without a Fortran main program unit,
you need to provide
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
in place of
make sure you specify the object file defining that procedure
-lg2c on the
g77 command line.
-lg2c option is implicitly provided,
this is usually straightforward.
--verbose option to see how and where
g77 implicitly adds
-lg2c in a command line
that will link the program.
Feel free to specify
However, when providing your own
make sure you perform the appropriate tasks in the
For example, if your
main does not call
make sure the rest of your application does not call
And, if your
main fails to ensure that
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
being called prior to any other
MAIN__ does not, itself,
set up any important operating-environment characteristics
upon which other code might depend.
This might change in future versions of
with appropriate notification in the release notes.
For more information, consult the source code for the above routines.
These are in
Also, the file
gcc/gcc/f/com.c contains the code
uses to open-code (inline) references to