Running a simple Fortran program
In the previous post I included the obligatory “hello world” snippet. It would be nice if you could actually run code like that on your computer! It’s not so hard; we have very modern tooling available, and everything works equally well on Windows and Linux. The tools are freely available too.
First, install the following programs:
- Intel oneAPI Fortan Esstentials. This includes the IFX compiler.
- Fortran Package Manager (FPM). This tool is a “zero-configuration” build system which can also install and build dependencies for you.
- Visual Studio Code. We’ll use this as our editor. As far as I know there is no fully-integrated coding solution for Fortran programs. Some may use Visual Studio, some Visual Studio Code, and Jetbrains Clion may be a good option too. But none of these have solutions for all your IDE needs. After some experimenting, I think VS Code is the best option.
In a terminal, navigate to a place where you would like to create your Fortran project, and run:
fpm new hello_world
This will create a folder hello_world
with the basic layout for an FPM project.
Now start VS Code and open the folder hello_world
that was just created by fpm
.
Install the following extensions from the Extensions panel (Ctrl + Shift + X
):
- Environment Configurator for Intel Software Developer Tools; this will set the right environment variables so the
ifx
compiler and its dependencies can be found. - Modern Fortran; this provides syntax highlighting, navigation and auto-completion. It also enables linting and formatting via external tools. More about this later. The extension is not signed by VS Code, so unfortunately you have to force installation via the little cogwheel. This extension requires Python to be installed on your machine, and it will install
fortls
, the Fortran Language Server.
Active environment: not selected?
Does the status bar of VS code show the message “Active environment: not selected”? Then take the following steps:
- Press
Ctrl + Shift + P
( orView - Command Palette...
) to open the Command Palette.- Type “Intel oneAPI” to view options of the installed extensions.
- Click on
Intel oneAPI: Initialize environment variables
.It should then find the location of oneAPI (e.g. in
Program Files
) and set the correct environment variables. In the status bar it says “Active environment: Default oneAPI config”.
It will be useful to provide some default settings for the VS code project. Make a folder .vscode
in your project and create a file called settings.json
there. These settings instruct the Modern Fortran extension to use ifx
as the compiler for linting the code (static analysis). This produces some artefacts, which I prefer to be stored inside the build/
folder instead of next to the Fortran files.
{
"fortran.linter.compiler": "ifx",
"fortran.linter.modOutput": "{workspaceFolder}/build/mod"
}
I find that often when you change extension configuration in this way, you’d have to restart the extension (Ctrl + Shift + P
, then run the command Developer: Restart Extension Host
).
You’ll find your Fortran application code (the “main” program) in app/main.f90
. This looks similar to the hello-world code shown earlier. Except, it includes a call to a function that lives in a different place, that is, inside the hello_world
module:
program main
use hello_world, only: say_hello
implicit none
call say_hello()
end program main
The code for the hello_world
module lives in src/hello_world.f90
. It looks as follows:
module hello_world
implicit none
private
public :: say_hello
contains
subroutine say_hello
print *, "Hello, hello_world!"
end subroutine say_hello
end module hello_world
We’ll look into the meaning of all this in another post.

To execute this simple program, first open a terminal in VS Code, then run this command:
fpm --compiler=ifx run
Do I have to add –compiler=ifx?
FPM defaults to compiling with
gfortran
. Theifx
compiler is supported, but you have to specify it everytime you usefpm
. I hope they’ll fix this at some point. It would be nice if we could simply store the compiler name as a setting in thefpm.toml
file.
As we learn from the output, FPM will find the program code, analyze at its dependencies (the use
statements), compile them first, then link the intermediate results together into a hello_world
executable.
+ mkdir -p build/dependencies
hello_world.f90 done.
libhello_world.a done.
main.f90 done.
hello_world done.
[100%] Project compiled successfully.
Hello, hello_world!
You’ll find the executable somewhere inside the build/
folder that was created. You don’t actually need to look it up though, because fpm
will already run it for you. You can see this in the output too: the last line shows the program’s output.
Whenever you change something in a source file and run fpm
again, it will only compile the files that were changed.
In the next post we’ll look closer at the files that FPM has generated, containing a program
and a module
, and how they work together.
This post is part of a series that helps you get started with Fortran if you already have some experience programming in another language. The series isn’t supposed to be a full introduction to Fortran. I only want to show the basics, after which you’ll know everything to dive into more advanced Fortran features that allow you to do Object-Oriented or Functional Programming with it (or a mix of both!). In future posts we’ll also explore strategies for keeping Fortran programs maintainable.