Clicky

Fortran Wiki
newbie

Prerequisites

To compile your first Fortran program, you’ll need

  • a Fortran compiler installed on your system, such as gfortran (part of the GNU Compiler Suite, freely available)

  • the compiler command placed in your command search path.

  • a terminal emulator you can open and can enter commands at the prompt in.

  • the contents of the “hello” program above copied into the file “hello.f90”.

Step-by-Step Guide

Here are the steps to create and compile the simple “Hello, World!” program:

  1. Create the Source File
  2. Open your text editor and type the following code:
     program hello
     implicit none
       print *, 'Hello, World!'
     end program hello

Save the File

Save the file with a .f90 extension as “hello.f90”.

Open Your Command Line Interface

Open your terminal window and navigate to the directory where you saved your file using the cd (change directory) command.

Compile the Program

Use your compiler from the command line. The basic command is:

    gfortran hello.f90 -o hello

where you substitute the name of your compiler for “gfortran”.

“-o hello” is an option that tells the compiler to name the resulting executable file “hello” (without this option, the default name would be a.out on Linux/macOS or a.exe on Windows).

If your program has syntax errors, the compiler will display error messages at this stage. If successful, it will simply return to the command prompt after creating the executable file.

Run the Executable

Execute your compiled program from the command line:

Linux/macOS:
    ./hello

Windows (Command Prompt):

    hello.exe

or just

    hello

The output will be:

Hello, World!

Next Steps

For more complex projects with multiple source files or specific options (like optimization or debugging flags), the command line syntax can be extended. Many programmers use build automation tools like make or an Integrated Development Environment (IDE) to manage these processes. The Fortran-lang website provides additional learning resources.