我怎样才能在Fortran语言使用dlmopen?

问题描述 投票:1回答:1

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

但是,我遇到了一些问题,我明白dlmopen应该,如果我使用的是相同的.so的多个实例可以使用。但是,我不知道如何来传递参数。有人可以帮助我如何做到这一点的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

更新:基于弗拉基米尔的建议,我下面尝试,但我得到qazxsw POI,

Unable to load DLL ./test.so - Third time
fortran posix gfortran dlopen
1个回答
1
投票

这是你可以从开始的基础,它是你的代码的修改,因为 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

根据手册,有可以传递为 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 ,无论dlistLM_ID_BASE两个主要的值。它们的值在位于ammong其他标准C和POSIX头(LM_ID_NEWLM或类似的)的标题dlfcn.h定义。你不应该只是通过1,但是发生在我的电脑上是0和-1这两个值中的一个。

© www.soinside.com 2019 - 2024. All rights reserved.