Fortran动态库,在运行时加载?

时间:2016-08-02 00:37:37

标签: macos fortran runtime shared-libraries intel-fortran

是否可以让Fortran程序在运行时加载Fortran库?如果是这样,是否可以修改函数并仅重新编译库以使最初编译的程序在运行时调用库中的修改函数?

如果有人能够提供一个如何实现这一目标的最小工作示例,那就太棒了。

1 个答案:

答案 0 :(得分:4)

以下是一些可能有用的链接:

  • 这个page on rosettacode.org给出了详细的完整示例,并讨论了在linux和MACOS上的实现
  • 这是intel forum post,其中Steve Lionel就如何使用ifort进行动态加载提出了一些建议
  • 这个IBM page对动态库及其用法有很好的解释

如果您想要一个易于理解的小代码,请继续阅读。几天前,我正在玩动态加载。我下面的测试代码可能对您有所帮助。但是我在linux环境中工作,你可能不得不在这里和那里调整一些东西,以便在你的OS X环境中工作。上面的rosettacode.org link可以帮助您。

以下是测试动态库的代码

[username@hostname:~/test]$cat test.f90

module test
    use, intrinsic :: iso_c_binding
contains
    subroutine t_times2(v_in, v_out) bind(c, name='t_times2')
        integer, intent(in) :: v_in
        integer, intent(out) :: v_out
        !
        v_out=v_in*2
    end subroutine t_times2
    !
    subroutine t_square(v_in, v_out) bind(c, name='t_square')
        integer(c_int), intent(in) :: v_in
        integer(c_int), intent(out) :: v_out
        !
        v_out=v_in**2
    end subroutine t_square
end module test

编译为

[username@hostname:~/test]$gfortran -c test.f90
[username@hostname:~/test]$gfortran  -shared -o test.so test.o

这是测试程序

[username@hostname:~/test]$cat example.f90
program example
    use :: iso_c_binding
implicit none

    integer(c_int), parameter :: rtld_lazy=1 ! value extracte from the C header file
    integer(c_int), parameter :: rtld_now=2 ! value extracte from the C header file
    !
    ! interface to linux API
    interface
        function dlopen(filename,mode) bind(c,name="dlopen")
            ! void *dlopen(const char *filename, int mode);
            use iso_c_binding
            implicit none
            type(c_ptr) :: dlopen
            character(c_char), intent(in) :: filename(*)
            integer(c_int), value :: mode
        end function

        function dlsym(handle,name) bind(c,name="dlsym")
            ! void *dlsym(void *handle, const char *name);
            use iso_c_binding
            implicit none
            type(c_funptr) :: dlsym
            type(c_ptr), value :: handle
            character(c_char), intent(in) :: name(*)
        end function

        function dlclose(handle) bind(c,name="dlclose")
            ! int dlclose(void *handle);
            use iso_c_binding
            implicit none
            integer(c_int) :: dlclose
            type(c_ptr), value :: handle
        end function
    end interface

    ! Define interface of call-back routine.
    abstract interface
        subroutine called_proc (i, i2) bind(c)
            use, intrinsic :: iso_c_binding
            integer(c_int), intent(in) :: i
            integer(c_int), intent(out) :: i2
        end subroutine called_proc
    end interface

    ! testing the dynamic loading
    integer i, i2
    type(c_funptr) :: proc_addr
    type(c_ptr) :: handle
    character(256) :: pName, lName

    procedure(called_proc), bind(c), pointer :: proc
    !
    i = 15

    handle=dlopen("./test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle))then
        print*, 'Unable to load DLL ./test.so'
        stop
    end if
    !
    proc_addr=dlsym(handle, "t_times2"//c_null_char)
    if (.not. c_associated(proc_addr))then
        write(*,*) 'Unable to load the procedure t_times2'
        stop
    end if
    call c_f_procpointer( proc_addr, proc )
    call proc(i,i2)
    write(*,*) "t_times2, i2=", i2
    !
    proc_addr=dlsym( handle, "t_square"//c_null_char )
    if ( .not. c_associated(proc_addr) )then
        write(*,*)'Unable to load the procedure t_square'
        stop
    end if
    call c_f_procpointer(proc_addr, proc)
    call proc(i,i2)
    write(*,*) "t_square, i2=", i2
contains
end program example

编译并运行为:

[username@hostname:~/test]$gfortran -o example example.f90 -ldl
[username@hostname:~/test]$./example
t_times2, i2=          30
t_square, i2=         225
[username@hostname:~/test]$
相关问题