3.3. GNU Fortran Compiler
3.3.1. Installing the Fortran Compiler
In Red Hat Developer Toolset, the GNU Fortran compiler is provided by the devtoolset-4-gcc-gfortran package and is automatically installed with devtoolset-4-toolchain as described in Section 1.5, “Installing Red Hat Developer Toolset”.
3.3.2. Using the Fortran Compiler
To compile a Fortran program on the command line, run the
gfortrancompiler as follows:
scl enable devtoolset-4 'gfortran -o output_file source_file...'
This creates a binary file named output_file in the current working directory. If the
-ooption is omitted, the compiler creates a file named
When you are working on a project that consists of several source files, it is common to compile an object file for each of the source files first and then link these object files together. This way, when you change a single source file, you can recompile only this file without having to compile the entire project. To compile an object file on the command line, run the following command:
scl enable devtoolset-4 'gfortran -o object_file -c source_file'
This creates an object file named object_file. If the
-ooption is omitted, the compiler creates a file named after the source file with the
.ofile extension. To link object files together and create a binary file, run:
scl enable devtoolset-4 'gfortran -o output_file object_file...'
Note that you can execute any command using the
sclutility, causing it to be run with the Red Hat Developer Toolset binaries used in preference to the Red Hat Enterprise Linux system equivalent. This allows you to run a shell session with Red Hat Developer Toolset
scl enable devtoolset-4 'bash'
To verify the version of
gfortranyou are using at any point, type the following at a shell prompt:
Red Hat Developer Toolset's
gfortranexecutable path will begin with
/opt. Alternatively, you can use the following command to confirm that the version number matches that for Red Hat Developer Toolset
Some newer library features are statically linked into applications built with Red Hat Developer Toolset to support execution on multiple versions of Red Hat Enterprise Linux. This adds a small additional security risk as normal Red Hat Enterprise Linux errata would not change this code. If the need for developers to rebuild their applications due to such an issue arises, Red Hat will signal this via a security erratum. Developers are strongly advised not to statically link their entire application for the same reasons.
Example 3.5. Compiling a Fortran Program on the Command Line
Consider a source file named
hello.fwith the following contents:
program hello print *, "Hello, World!" end program hello
To compile this source code on the command line by using the
gfortrancompiler from Red Hat Developer Toolset, type:
scl enable devtoolset-4 'gfortran -o hello hello.f'
This creates a new binary file called
helloin the current working directory.
3.3.3. Running a Fortran Program
gfortrancompiles a program, it creates an executable binary file. To run this program on the command line, change to the directory with the executable file and type:
Example 3.6. Running a Fortran Program on the Command Line
Assuming that you have successfully compiled the
hellobinary file as shown in Example 3.5, “Compiling a Fortran Program on the Command Line”, you can run it by typing the following at a shell prompt: