Skip to main content
Engineering LibreTexts

3.4: Compiling

  • Page ID
    54245
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Once the program is typed into a file, the file must be compiled. Compiling will convert the human-readable Fortran program, or source file, into a machine-readable version (in binary).

    In order to compile, the command prompt (Windows) or terminal interface (Unix, MAC) is required. This interface will allow commands to be typed directly into the computer (instead of using a mouse). Once started, it is typically necessary to change directories (from the default location) to the location of where the hw.f95 source file was located (from the previous steps). Changing directories is typically done with a cd <directoryName> command. For example, cd fortran (which is the name of the directory used in this example). The prompt typically changes to include the current directory location.

    In the example below, the commands typed by the user are displayed in bold. The regular (non-bolded) text refers to prompts or other information displayed by the computer (which need not be typed). To compile the example program, the following command would be entered:

    C:\fortran> gfortran -o hw hw.f95
    

    This command will tell the 'gfortran' compiler to read the file hw.f95 and, if there are no errors, create an output file referred to as an executable file. On Windows based machines the executable file is named hw.exe. And on Unix or Mac based machines, the executable is named hw (no extension). If there is an error, the compiler will generate an error message, sometimes cryptic, and provide a line number. Such errors are usually the result of mistyping one of the instructions. Any errors must be resolved before continuing.

    Advanced Compiler Options

    In addition, to the basic compilation options, sometimes additional instructions, or options, may be required. For example, when using arrays (chapter 9), an option for bounds checking is typically desired.

    For example, to compile with bounds checking, the following command might be entered:

    C:\fortran> gfortran -fcheck=bounds -o hw hw.f95
    

    This command will tell the 'gfortran' compiler to include bounds checking. More information regarding arrays and bounds checking is addressed in chapters 13 and 14.


    This page titled 3.4: Compiling is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?