In this section, we will guide you through the process of setting up your development environment for Fortran programming. This includes installing the necessary software, configuring your editor or IDE, and verifying that everything is working correctly.

  1. Installing a Fortran Compiler

To write and run Fortran programs, you need a Fortran compiler. The most commonly used Fortran compilers are:

  • GNU Fortran (gfortran): Part of the GNU Compiler Collection (GCC), it is free and open-source.
  • Intel Fortran Compiler (ifort): A high-performance compiler, but it requires a license.
  • NAG Fortran Compiler: Known for its strong standards compliance and error-checking capabilities.

Installing GNU Fortran (gfortran)

On Windows:

  1. Download and Install MinGW:

    • Go to the MinGW website.
    • Download the MinGW Installation Manager.
    • Run the installer and select gfortran for installation.
  2. Set Environment Variables:

    • Add the path to the MinGW bin directory to your system's PATH environment variable.

On macOS:

  1. Install Homebrew (if not already installed):

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. Install gfortran:

    brew install gcc
    

On Linux:

  1. Use the package manager:
    • For Debian-based systems (e.g., Ubuntu):
      sudo apt-get update
      sudo apt-get install gfortran
      
    • For Red Hat-based systems (e.g., Fedora):
      sudo dnf install gcc-gfortran
      

Verifying the Installation

After installing the compiler, verify the installation by checking the version:

gfortran --version

You should see output indicating the version of gfortran installed.

  1. Choosing an Editor or IDE

While you can write Fortran code in any text editor, using an Integrated Development Environment (IDE) can make the process more efficient. Here are some popular options:

  • Visual Studio Code: A free, open-source editor with extensions for Fortran.
  • Eclipse with Photran: An IDE with a dedicated Fortran plugin.
  • Code::Blocks: A free C, C++, and Fortran IDE.

Setting Up Visual Studio Code for Fortran

  1. Install Visual Studio Code:

  2. Install Fortran Extensions:

    • Open Visual Studio Code.
    • Go to the Extensions view by clicking the Extensions icon in the Activity Bar on the side of the window.
    • Search for "Modern Fortran" and install it.
  3. Configure the Fortran Extension:

    • Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P on macOS).
    • Type Preferences: Open Settings (JSON) and select it.
    • Add the following configuration to the settings file:
      {
        "fortran.linter.compiler": "gfortran",
        "fortran.linter.compilerPath": "/path/to/gfortran"
      }
      
    • Replace /path/to/gfortran with the actual path to your gfortran executable.

  1. Writing and Running Your First Fortran Program

Let's write a simple Fortran program to ensure everything is set up correctly.

Example Program: Hello World

  1. Create a new file named hello.f90 in your editor.

  2. Write the following code:

    program hello
        print *, "Hello, World!"
    end program hello
    
  3. Save the file.

Compiling and Running the Program

  1. Open a terminal in the directory where hello.f90 is saved.

  2. Compile the program:

    gfortran -o hello hello.f90
    

    This command compiles hello.f90 and creates an executable named hello.

  3. Run the executable:

    ./hello
    

    You should see the output:

    Hello, World!
    

Conclusion

In this section, you have successfully set up your Fortran development environment by installing a Fortran compiler, configuring an editor or IDE, and writing and running a simple Fortran program. This setup will serve as the foundation for all your Fortran programming tasks throughout this course. In the next section, we will dive into the basic syntax and structure of Fortran programs.

© Copyright 2024. All rights reserved