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

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
1 contribution / 0 nouveau(x)
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.