【问题标题】:Fortran dynamic libraries, load at runtime?Fortran 动态库,在运行时加载?
【发布时间】:2016-12-07 04:45:13
【问题描述】:

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

如果有人可以提供一个最小的工作示例来说明如何实现这一点,那就太好了。

【问题讨论】:

  • 问:是否可以让 fortran 程序在运行时加载 fortran 库?答:当然。例如 Gnu Fortran 在 Linux 上支持动态模块 (.so)。
  • 你问的是如何创建动态库?
  • 链接器是否需要新的库或者知道该怎么做?使用 .a 和 a .so 的情况有所不同。我假设您无法重新编译其他部分,或者没有源代码?
  • 这是 Linux 还是 Windows?任何一个都有可能。在 Linux 上,您调用 dlopen 和 dlsym 来获取指向例程的指针,在 Windows LoadLibrary 和 GetProcAddress 上。英特尔 Visual Fortran 为 Windows 提供了一个完整的示例。
  • Steve,它实际上是在 OS X 上。

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


【解决方案1】:

这里有一些有用的链接:

  • 这个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]$

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多