<h1 align="center">
gfortran: Using the Compiler
</h1>
<h2>Contents</h2>
<ul>
<li><a href="http://gcc.gnu.org/fortran/usage.html#intro" target="_blank" >Introduction</a></li><li><a href="http://gcc.gnu.org/fortran/usage.html#bu" target="_blank" >Basic Usage</a></li><li><a href="http://gcc.gnu.org/fortran/usage.html#ao" target="_blank" >Advanced Options</a>
<ul><li><a href="http://gcc.gnu.org/fortran/usage.html#fixed" target="_blank" >Compiling Fixed Form Sources</a></li><li><a href="http://gcc.gnu.org/fortran/usage.html#fname" target="_blank" >Compiling Files Not Named
<code>*.f9[05]</code></a></li><li><a href="http://gcc.gnu.org/fortran/usage.html#g77" target="_blank" >Compatibility With g77</a></li><li><a href="http://gcc.gnu.org/fortran/usage.html#fend" target="_blank" >Running the Compiler front end</a></li></ul>
</li>
</ul>
<h2><a>Introduction</a></h2>
<br>
This page is meant to give a quick overview on how to use gfortran. It
is merely a substitute for a complete manual, which has not yet been
written.
</p>
<p>
Gfortran builds on GCC, and thus
shares most characteristics with it. If you know how to use GCC, there
will not be much new information for you in this document. Especially,
the options for optimization and the generation of debugging
information are not outlined here.
</p>
<h2><a>Basic Usage</a></h2>
<p>
Gfortran is used to compile a source file, <code>source.f90</code>, to
an object file, <code>object.o</code>, or an executable,
<code>executable</code>. Along the way it generates module description
files for the modules it encounters, these are named
<code><i>nameofmodule</i>.mod</code>. If a module is used, gfortran will
read from these same files.
</p>
<p>
In order to compile the source file <code>source.f90</code>,
one would run: <code>gfortran -c source.f90</code><br>
The output file will automatically be named
<code>source.o</code>. This is an object file, which cannot be executed.
</p>
<p>
Once you have compiled some source files, you can link them
together with the necessary libraries to generate an executable. This
is done as follows: <code>gfortran -o executable object1.o object2.o
...</code>, where the the executable will be named
<code>executable</code> and the <code>objectX.o</code> are object
files, which may have been created as above, or equally well by another
compiler from sources in a different language. If <code>-o
executable</code> is omitted, the executable will be named
<code>a.out</code> (on cygwin systems: <code>a.exe</code>). The
executable may then be executed like any other program.
</p>
<p>
One may also skip the separate compilation step, and enter a command
such as <code>gfortran -o executable source1.f90 source2.f90</code> which
will compile the source files <code>source1.f90</code> and
<code>source2.f90</code>, link and generate the executable
<code>executable</code> in one step. You can also put object files on this
command line, they will be automatically linked in during the link step.
</p>
<h2><a>Advanced Options</a></h2>
<p>
Sometimes the basic possibilities given above do not match the user's
needs. Therefore this section outlines other stuff the user might
want to know.
</p>
<h3><a>Compiling Fixed Form Sources</a></h3>
<p>
When <code>gfortran</code> is run on a file, whose name ends in
<code>.f90</code> or
<code>.f95</code>, gfortran assumes a free form source file. If that file
actually is a fixed form source file, the user has to give the
<code>-ffixed-form</code> command line option. The precise semantics
of this option, and other options relating to fixed form versus free form
input are the same as in g77, and may be found in g77's documentation.
</p>
<h3><a>Compiling Files Not Named <code>*.f9[05]</code></a></h3>
<p>
When running <code>gfortran</code> one actually does not run the compiler,
but the compiler driver. This driver interprets the command
line options given, and hands the work off to the actual compiler,
the assembler, and the linker. By default, this compiler driver
decides by the extensions of the given file names what to do. A file
named <code>foo.c</code> is handed to the C compiler and a file named
<code>moo.f90</code> is handed to the Fortran 95 compiler, etc. To
overrule this behavior, one has to precede the filename by the argument
<code>-x <i>lang</i></code>, where <code><i>lang</i></code> is a string
identifying the requested language. For Fortran 95 this is
<code>f95</code>.
</p>
<p>
Since Fortran allows for two different kinds of input, free form
source code and fixed form source code, the compiler has to know which
kind of input it is given. The rule in place is as follows: files whose name
ends in <code>.f</code> or <code>.for</code> are assumed to be fixed
form, files whose name end in <code>.f90</code> or <code>.f95</code>
are assumed to to be free form, for other files the source form has to
be explicitly given. This may be done by the command line options
described above, which may also be used to override these rules.
</p>
<h3><a>Compatibility with g77</a></h3>
<p>
In order to efficiently implement the passing of array sections,
binary compatibility to Fortran 77 had to be abandoned. If the user
wishes to link his sources with old Fortran 77 codes, the command line
option <code>-fg77-calls</code> changes back to the old calling
convention used by g77.
</p>
<p>
When linking with code compiled by g77, one also has to take care, because
g77 and gfortran use different libraries. Especially I/O might get messed
up due to this. Your safest bet is to only use I/O in either the g77 compiled
parts or the gfortran compiled parts, but not both, and to use the compiler
driver of the part which uses I/O in the final link step. There might be
circumstances where doing I/O in both works, but there is nothing
guaranteed. In the final link step you should also explicitly specify
the libraries of both compilers, i.e. <code>-lgfortran</code> for gfortran,
<code>-lg2c</code> for g77.
</p>
<h3><a>Running the Compiler Frontend</a></h3>
<p>
One may use gfortran as a syntax checker (or verify that gfortran's frontend
correctly accepts or rejects a program), by specifying
<code>-fsyntax-only</code> on the command line. Gfortran will then not
generate object files.
</p>
<p>
When given the command line option <code>-fdump-parse-tree</code>,
gfortran will print a representation of the parsed program, detailing both
the data objects and the executable statements of the program in a
Lisp-inspired notation. One remark for Fortran old timers:
<code>ASSIGN</code> in these dumps does not refer to the
<code>ASSIGN</code> statement, but to the operation of assignment,
i.e. sloppily speaking, the <code>=</code> operator.
</p>
<hr>
<p>Please send FSF & GNU inquiries & questions to
<a href="mailtgnu@gnu.org" target="_blank" >gnu@gnu.org</a>.
There are also <a href="http://www.gnu.org/home.html#ContactInfo" target="_blank" >other ways
to contact</a> the FSF.</p>
<p>These pages are maintained by
<a href="http://gcc.gnu.org/about.html" target="_blank" >the GCC team</a>.</p>
<address>For questions related to the use of GCC, please consult these web
pages and the <a href="http://gcc.gnu.org/onlinedocs/" target="_blank" >GCC manuals</a>. If
that fails, the <a href="mailtgcc-help@gcc.gnu.org" target="_blank" >gcc-help@gcc.gnu.org</a>
mailing list might help.<br>
Please send comments on these web pages and the development of GCC to our
developer mailing list at <a href="mailtgcc@gnu.org" target="_blank" >gcc@gnu.org</a>
or <a href="mailtgcc@gcc.gnu.org" target="_blank" >gcc@gcc.gnu.org</a>. All of our lists
have <a href="http://gcc.gnu.org/lists.html" target="_blank" >public archives</a>.
</address>
<p>Copyright (C) Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110, USA.</p>
<p>Verbatim copying and distribution of this entire article is
permitted in any medium, provided this notice is preserved.</p>
<!-- IGNORE DIFF -->Last modified 2005-07-11 |