2.3. GNU Fortran Compiler
2.3.1. Installing the Fortran Compiler
In Red Hat Developer Toolset, the GNU Fortran compiler is provided by the devtoolset-6-gcc-gfortran package and is automatically installed with devtoolset-6-toolchain as described in Section 1.5, “Installing Red Hat Developer Toolset”.
2.3.2. Using the Fortran Compiler
To compile a Fortran program on the command line, run the
gfortran compiler as follows:
scl enable devtoolset-6 'gfortran -o output_file source_file...'
This creates a binary file named output_file in the current working directory. If the
-o option is omitted, the compiler creates a file named
a.out by default.
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-6 'gfortran -o object_file -c source_file'
This creates an object file named object_file. If the
-o option is omitted, the compiler creates a file named after the source file with the
.o file extension. To link object files together and create a binary file, run:
scl enable devtoolset-6 'gfortran -o output_file object_file...'
Note that you can execute any command using the
scl utility, 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
gfortran as default:
scl enable devtoolset-6 'bash'
To verify the version of
gfortran you are using at any point, type the following at a shell prompt:
Red Hat Developer Toolset's
gfortran executable 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 2.5. Compiling a Fortran Program on the Command Line
Consider a source file named
hello.f with the following contents:
program hello print *, "Hello, World!" end program hello
To compile this source code on the command line by using the
gfortran compiler from Red Hat Developer Toolset, type:
scl enable devtoolset-6 'gfortran -o hello hello.f'
This creates a new binary file called
hello in the current working directory.
2.3.3. Running a Fortran Program
gfortran compiles 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 2.6. Running a Fortran Program on the Command Line
Assuming that you have successfully compiled the
hello binary file as shown in Example 2.5, “Compiling a Fortran Program on the Command Line”, you can run it by typing the following at a shell prompt: