Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Calling atexit() from Fortran

mriedman
Novice
772 Views
I'm not sure whether or not there is a more elegant way of implementing module finalizer routines in Fortran. If yes, please let me know.

Otherwise feel free to use thisdemo code which makes use of the ISO C bindings and POSIX atexit() function and demonstrates how Fortran routines can be passed as an argument to atexit().

[fortran]module atexit_demo

  use, intrinsic :: iso_c_binding

  implicit none

interface

  function atexit(fptr) bind(c)
  use, intrinsic :: iso_c_binding
  type(c_funptr), value, intent(in) :: fptr
  integer(4) :: atexit
  end function

end interface

  public

contains

  subroutine init
    integer :: ierr
    type(c_funptr) :: fptr

    fptr = c_funloc(called_on_exit)
    print *,'calling atexit'
    ierr = atexit(fptr)
    print *,'atexit error', ierr
  end subroutine init


  subroutine called_on_exit
    print *,'Bye'
  end subroutine called_on_exit

end module atexit_demo
[/fortran]
0 Kudos
0 Replies
Reply