【问题标题】:How can I use dlmopen in Fortran?如何在 Fortran 中使用 dlmopen?
【发布时间】:2019-02-06 09:20:05
【问题描述】:

我想将相同的 .so 文件作为单独的实例加载两次。基于example,我用两个dlopen 命令创建了应用程序。

但是,我遇到了一些问题,我知道如果我使用相同 .so 的多个实例,应该使用dlmopen。但是,我不知道如何传递论点。有人可以帮我在 GFortran 中如何做到这一点吗?

我的代码如下,

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) :: handle1, handle2
    character(256) :: pName, lName

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

    handle1=dlopen("./test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle1))then
        print*, 'Unable to load DLL ./test.so - First time'
        stop
    end if
    handle2=dlopen("./test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle2))then
        print*, 'Unable to load DLL ./test.so - Second time'
        stop
    end if

    ! If I can use dlmopen() I dont know how to pass the arguments

    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

更新: 根据弗拉德米尔的建议,我在下面尝试了,但我得到了Unable to load DLL ./test.so - Third time

        function dlmopen(lmid_t,filename,mode) bind(c,name="dlmopen")
            ! void *dlopen(const char *filename, int mode);
            use iso_c_binding
            implicit none
            type(c_ptr) :: dlopen
            integer(c_long), value :: lmid_t
            character(c_char), intent(in) :: filename(*)
            integer(c_int), value :: mode
        end function


    handle3=dlmopen(1,"./test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle3))then
        print*, 'Unable to load DLL ./test.so - Third time'
        stop
    end if

【问题讨论】:

  • 你有dlmopen的接口吗?
  • 你还需要你的老问题stackoverflow.com/questions/53927166/…吗?还是没有要在那里添加细节以使其可以回答?
  • 考虑posix或相关标签}也许是关于动态库的东西)来吸引那些熟悉这些功能的人。
  • @Selva,你明白lmid 是做什么的吗?您应该查看系统标头中的 Lmid_t 是什么,但不能保证它在其他系统上也一样。
  • @VladimirF - 感谢您的回复。是的,它们很相似,当我开始使用它时,我什至不知道 dlopen 存在。无论如何,我想我会结束这个问题。我将添加 posix 标签。至于 lmid,我不知道 lmid 是什么以及如何查看标题

标签: fortran posix gfortran dlopen


【解决方案1】:

这是您可以开始的基础,它是对您的代码的修改,并且由于#define 宏,它需要cpp 标志。如果需要,您可以将定义更改为普通参数声明,但仅从标头复制宏更容易。

    use iso_c_binding

    implicit none

! from dlfcn.h
# define LM_ID_BASE 0   /* Initial namespace.  */
# define LM_ID_NEWLM    -1  /* For dlmopen: request new namespace.  */

    integer(c_long) :: dlist = LM_ID_NEWLM
    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
    type(c_ptr) :: handle3     

    interface
        function dlmopen(lmid_t,filename,mode) bind(c,name="dlmopen")
            ! void *dlmopen (Lmid_t lmid, const char *filename, int flags);
            use iso_c_binding
            implicit none
            type(c_ptr) :: dlmopen
            integer(c_long), value :: lmid_t
            character(c_char), intent(in) :: filename(*)
            integer(c_int), value :: mode
        end function
    end interface

    handle3=dlmopen(dlist,"test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle3))then
        print*, 'Unable to load DLL ./test.so - Third time'
        stop
    end if
end    

根据手册,您可以将两个主要值作为dlist 传递,LM_ID_BASELM_ID_NEWLM。它们的值在标头 dlfcn.h 中定义,该标头位于其他标准 C 和 POSIX 标头(/usr/include/ 或类似标头)中。您不应该只传递 1,而应该传递这两个值之一,在我的计算机上恰好是 0 和 -1。

【讨论】:

  • 谢谢弗拉德米尔。这可行,但我遇到了与 dlopen 相同的问题。看起来有些变量在 .so 之间共享,尽管我已将它们放在两个不同的文件夹中。我看到 rtld_local=8 应该可以工作,但如果我给它,我会得到'Unable to load DLL ./test.so - Third time
  • 您是否使用了LM_ID_NEWLM 值?我认为您将不得不创建一个仅关于共享问题的新问题。这是 POSIX 专家的东西,Fortran 人一般不知道这些东西。
  • 我确实尝试了 LM_ID_NEWLM 但同样的问题。我的 .so 确实使用 dlopen 加载了一些辅助 .so 文件,但不确定是不是因为这个。我无法修改 .so 源 :( 正如您所建议的,我会提出一个新问题并将其标记为答案。非常感谢您的支持
  • @Selva 如果你懂 C,我会先在 C 中尝试同样的事情。如果你不懂 C,你别无选择...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
  • 2012-11-30
  • 1970-01-01
相关资源
最近更新 更多