Fortran Standard Library

The Fortran Standard, as published by the ISO, does not have a standard library, like other programming languages. The Fortran Standard Library (stdlib) is a community-driven de facto standard library currently in development that contains additional algorithms, data structures, as well as math and utility routines for Fortran 2008 (table 1).

ModuleDescription
stdlib_asciiHandling of intrinsic character variables and constants
stdlib_bitsetsZero-based bitsets of size up to huge(0_int32)
stdlib_errorCatching and handling errors
stdlib_ioFile handling
stdlib_kindsAdditional kind declarations
stdlib_linalgVarious linear algebra procedures
stdlib_loggerLogging types and procedures
stdlib_mathVarious math procedures
stdlib_optvalFallback values for optional procedure arguments
stdlib_quadratureGaussian approximations
stdlib_randomRandom numbers
stdlib_sortingSorting algorithms
stdlib_specialfunctionsLegendre functions
stdlib_statsVarious statistical methods
stdlib_stats_distribution_uniformUniform distribution functions.
stdlib_string_typeString type that holds an arbitrary sequence of characters
stdlib_stringlist_typeString list implementation
stdlib_stringsBasic string handling routines
stdlib_systemSystem procedures
Tab. 1: Modules of the Fortran Standard Library

Installation

The library requires a Fortran 2008 compliant compiler and a build automation tool like fpm, make, or CMake:

$ git clone https://github.com/fortran-lang/fpm
$ git checkout stdlib-fpm
$ fpm build --profile release

The static library libstdlib.a and the necessary module files are written to build/<compiler>/stdlib/. To include stdlib as a dependency to a fpm project, simply add the following entry to your fpm.toml:

[dependencies]
stdlib = { git="https://github.com/fortran-lang/stdlib", branch="stdlib-fpm" }

stdlib_logger

The Fortran Standard Library provides a basic logger class to output messages to console and file:

! log.f90
program main
    use, intrinsic :: iso_fortran_env, stderr => error_unit
    use :: stdlib_logger, logger => global_logger
    implicit none
    integer :: fu, rc

    call logger%configure(level=ALL_LEVEL)
    call logger%add_log_unit(stderr, stat=rc)
    call logger%add_log_file('log.txt', fu, stat=rc)

    call logger%log_error(message   = 'Example error log message', &
                          module    = 'N/A', &
                          procedure = 'MAIN', &
                          stat      = 0, &
                          errmsg    = 'error message')

    call logger%log_message('Message printed irrespective of the level')

    call logger%log_debug('Debug message')
    call logger%log_information('Information message')
    call logger%log_warning('Warning message')
    call logger%log_io_error('I/O error message')

    call logger%configure(level=NONE_LEVEL)
    call logger%log_error('Error message (not printed)')
end program main

Link the example program against libstdlib.a and set the include search path -I the the directory containing the module files:

$ gfortran10 -I./build/gfortran/stdlib/ -o log log.f90 ./build/gfortran/stdlib/libstdlib.a

The log messages are printed to standard error and to file log.txt:

$ ./log
2021-10-04 23:04:46.090: N/A % MAIN: ERROR: Example error log message
With stat = 0
With errmsg = "error message"
2021-10-04 23:04:46.091: Message printed irrespective of the level
2021-10-04 23:04:46.091: DEBUG: Debug message
2021-10-04 23:04:46.091: INFO: Information message
2021-10-04 23:04:46.091: WARN: Warning message
2021-10-04 23:04:46.091: I/O ERROR: I/O error message

Similar Fortran Libraries

References