Calling atexit() from Fortran

Calling atexit() from Fortran

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().

module atexit_demo

  use, intrinsic :: iso_c_binding

  implicit none


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

end interface



  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
1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.