concept iso_fortran_env in category fortran

appears as: iso_fortr_env
Modern Fortran: Building efficient parallel applications

This is an excerpt from Manning's book Modern Fortran: Building efficient parallel applications.

Before we venture into writing our own, custom modules, let’s first get familiar by accessing a built-in module that comes with Fortran out of the box. Fortran has a few built-in modules. The most commonly used one is iso_fortran_env, which, among other things, provides constants and procedures that allow you to write more portable programs. Another commonly used module is iso_c_binding, which allows you to interface functions and data in C and other languages. We’ll dig more into that in chapter 12.

Once you have a compiled program executable, it’s not obvious how it was compiled. Specifically, what compiler was used, and were any compiler options used, for example, for debugging or optimization? Fortran’s iso_fortran_env module provides two functions that allow you to get this information at runtime: compiler_version and compiler_options. You get the idea what each of these functions does. Let’s write a program that imports these functions from the iso_fortran_env module, then call them to print this information to the screen. Figure 4.1 illustrates how accessing a module from a main program works, with the new syntax marked in bold.

Figure  4.1. Importing variables and procedures from a module into a main program
figure iso fortran env

Once you’ve accessed the module via the use statement, you can use directly in the program any variables or procedures that the module makes available. You also can do this from a function, a subroutine, or another module. (We’ll get to this in a bit.) The following listing provides the complete program that imports two functions from the iso_fortran_env module and calls them on two print statements.

Listing 4.1. A program that prints the compiler version and options at runtime
language="fortran" linenumbering="unnumbered">program print_compiler_info
  use iso_fortran_env #1
  implicit none
  print *, 'Compiler version: ', compiler_version() #2
  print *, 'Compiler options: ', compiler_options() #3
end program print_compiler_info

On the second line of this program, we access the built-in iso_fortran_env module with the use statement. This module is available in Fortran out of the box. The use statement means that we want to access entities—variables and/or procedures—from the module. This statement imports every entity that’s defined in the module, including the two functions that we’re looking for.