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

Shared library and DEC ALIAS issue with underscore

Jacob_Williams
New Contributor I
486 Views

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.

0 Kudos
2 Replies
FortranFan
Honored Contributor II
486 Views

You may want to look into DECORATE option with !DIR$ ATTRIBUTES and see if that provides a "portable" approach across the 3 platforms:

https://software.intel.com/en-us/fortran-compiler-developer-guide-and-reference-attributes-decorate

module test_module

    implicit none

contains

    subroutine execute(id)
    !DIR$ ATTRIBUTES DECORATE :: execute ! Does this work?

    integer,intent(in) :: id

    write(*,*) '[execute] id = ', id

    end subroutine execute

end module test_module

 

0 Kudos
Jacob_Williams
New Contributor I
486 Views

Ah ha! DECORATE by itself doesn't work, but when I use both like this:

!DEC$ ATTRIBUTES DECORATE,ALIAS : "execute" :: execute

It does work (at least on Mac). Still need to check Windows and Linux. Thanks!

0 Kudos
Reply