CMake builds for modern Fortran 2008/2018

Fortran languages standards keep long backward compatibility (e.g Fortran 2018 allows Fortran 1958 fixed width format). Fortran compilers, like on C++, take years to add the full feature set, if ever. Automatically determining if a particular compiler supports a needed modern Fortran feature is very straightforward with CMake.

This particular example is for Fortran 2008 error stop.

Example CMakeLists.txt is below. Unlike C++, we do not typically need to enable modern Fortran features with specific compiler flags. The modern Fortran features are enabled by default across all commonly used Fortran compilers including:

  • Gfortran
  • PGI
  • Flang
  • Intel
  • Cray
  • NAG
  • …and more
cmake_minimum_required(VERSION 3.1)
project(es Fortran)

include(CheckFortranSourceCompiles)
check_fortran_source_compiles("program es; error stop; end" f2008
                              SRC_EXT f90)
if(f2008)
  add_executable(myprog myprog.f90)
endif()

Notes

Tags:

Categories:

Comments

Written by Michael Hirsch, Ph.D. //