Skip to main content
Engineering LibreTexts

22.4: MacOS Compilation

  • Page ID
    54394
  • \( \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}}\)

    Please note, the Fortran compiler and xCode download and installation (previous section) must have been completed before starting this step. In order to compile and work with programs under MacOS, we will need to provide typed commands to the computer. This is done from within the Terminal window.

    Terminal Window

    The first step is to open the Terminal window. The open Terminal window will look similar to the following:

    A macOS terminal.

    The screen background may be black depending on the default configuration settings. Once the Terminal is open, the directory for the working files can be set as shown in the following section.

    Device and Directory

    In order to compile and work with programs under MacOS, a working directory should be established. This is where the program files will be stored.

    At the Terminal window prompt (i.e., “eds-iMac:~ ed$” in this example) you will need to change directory into the directory where your files are (if you created one). The Terminal prompt will be different for each user and includes a combination of the machine name and the current user account name. The “cd <dir_name>” command can be used. For example, if the directory is named cs117, the command would be “cd cs117”.

    For example:

    eds-iMac:~ ed$ cd cs117
    eds-iMac:cs117 ed$
    

    The prompt will change to show the new directory name (second line). At this point, typing ls (for directory) will provide a list of files in the directory. While the format will appear different, the files are the same as shown in the Finder listing.

    Compiler Installation Verification

    To ensure the compiler is installed correctly, open the Terminal window, and type gfortran at the prompt.

    eds-iMac:cs117 ed$ gfortran
    gfortran: fatal error: no input files
    compilation terminated.
    

    The “no input files” message means that the installation was completed successfully.

    However, if the following message is displayed,

    eds-iMac:cs117 ed$ gfortran
    -bash: gfortran: command not found
    

    it means that the Fortran compiler is not installed. The installation issue must be addressed before continuing.

    If the following message is displayed,

    eds-iMac:cs117 ed$ gfortran -o hw hw.f95
    xcode-select: note: no developer tools were found at
    '/Applications/Xcode.app', requesting install. Choose an option
    in the dialog to download the command line developer tools.
    

    it means that the Fortran compiler is installed, but Xcode is not installed. Xcode must be successfully installed in order to continue. Once the Fortran and Xcode installations are completed successfully, the compilation steps detailed in Chapter 3 can be performed.

    Compilation

    Once the Fortran compiler and Xcode are installed, programs can be compiled. Open the Terminal window and change directory as appropriate.

    At this point the program can be compiled, using “gfortran. The optional “-o” qualifier is used to set the name of the output file. To execute, you can use the name of the executable (that was specified with the “-o”).

    To compile the example program, the following command would be entered:

    eds-iMac:cs117 ed$ 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 executable file 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 or more of the instructions. Any errors must be resolved before continuing.

    Executing

    To execute or run a program, type “./” and the name of the executable file. For example, to execute or run the hw program:

    eds-iMac:cs117 ed$ ./hw
     Hello World
    eds-iMac:cs117 ed$
    

    Which will execute the example program and display the “Hello World” message to the screen.

    Example

    A more complete example is as follows:

    Example of compilation in the macOS terminal.

    The “./” is required when executing programs.


    This page titled 22.4: MacOS Compilation 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.