I'm using dlopen and dlsym to open a shared library and execute a procedure. It works fine on Windows and Linux, but I see a weird issue on Mac. Consider the following example:
Main code (main.f90):
program main use, intrinsic :: iso_c_binding implicit none procedure(func),pointer :: execute !! procedure in the shared lib integer(c_intptr_t) :: dll_handle integer(c_intptr_t) :: procaddress integer(c_int),parameter :: rtld_lazy = 1 abstract interface subroutine func(id) implicit none integer,intent(in) :: id end subroutine func end interface interface function dlopen(library, iflag) result(handle) bind(c, name='dlopen') import implicit none integer(c_intptr_t) :: handle character(kind=c_char),intent(in),dimension(*) :: library integer(kind=c_int),intent(in),value :: iflag end function dlopen function dlsym(handle, method) result(funptr) bind(c, name='dlsym') import implicit none integer(c_intptr_t) :: funptr integer(c_intptr_t),intent(in),value :: handle character(kind=c_char),intent(in),dimension(*) :: method end function dlsym end interface dll_handle = dlopen('test.so'//c_null_char, rtld_lazy) procaddress = dlsym(dll_handle, 'execute'//c_null_char) call c_f_procpointer(transfer(procaddress,c_null_funptr), execute) call execute(42) end program main
Shared library code (test.f90):
module test_module implicit none contains subroutine execute(id) ! -- DEC$ ATTRIBUTES ALIAS : "execute" :: execute ! doesn't work !DEC$ ATTRIBUTES ALIAS : "_execute" :: execute ! works integer,intent(in) :: id write(*,*) '[execute] id = ', id end subroutine execute end module test_module
Built using:
ifort -dynamiclib test.f90 -o test.so ifort main.f90 -o main
I'm unable to access the procedure as "execute" without prepending an underscore to the DEC ALIAS command. This is not necessary on Windows or Linux. Is there some DEC magic that will allow me not to have to add this? Is this a bug in the Mac compiler?
Note that I think bind(c) also makes it work, but I can't use that for my real code since the procedure contains non-interoperable arguments.