如何在Fortran中使用dlmopen? [英] How can I use dlmopen in Fortran?

查看:115
本文介绍了如何在Fortran中使用dlmopen?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想两次将相同的 .so 文件作为单独的实例加载.根据示例,我使用两个dlopen命令创建了该应用

I want to load the same .so file twice as separate instances. Based on the example, I have created the app with two dlopen commands.

但是,我遇到了一些问题,并且我了解到 .so 的多个实例,则应使用> .但是,我不知道如何传递参数.有人可以帮我在GFortran中做到这一点吗?

However, I was facing some issues and I understood that dlmopen should be used if I am using multiple instances of a same .so. However, I don't know how to pass the arguments. Can someone help me how to do this in 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

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

Update: Based on the suggestion from Vladmir, I tried below but I get 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

推荐答案

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

This is the base that you can start from, it is a modification of your code and because of the #define macros it requires the cpp flags. You can change the defines to a normal parameter declaration if you want, but just copying the macros from the header is easier.

    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.它们的值在其他标准C和POSIX标头(/usr/include/或类似名称)中的标头dlfcn.h中定义.您不仅应该传递1,还应该传递这两个值之一,它们在我的计算机上恰好是0和-1.

According to the manual, there are two principal values that you can pass as dlist, either LM_ID_BASE or LM_ID_NEWLM. Their values are defined in the header dlfcn.h that is located ammong other standard C and POSIX headers (/usr/include/ or similar). You should not just pass 1, but one of these two values which happen to be 0 and -1 on my computer.

这篇关于如何在Fortran中使用dlmopen?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆