len*_*nho 4 macos fortran runtime shared-libraries intel-fortran
是否可以让 Fortran 程序在运行时加载 Fortran 库?如果是这样,是否可以修改一个函数并只重新编译库,让原来编译的程序在运行时调用库中修改后的函数?
如果有人能提供一个关于如何实现这一点的最小工作示例,那就太好了。
以下是一些有用的链接:
如果您想要一个易于理解的小代码,请继续阅读。前几天,我在玩动态加载。我下面的测试代码可能对您有所帮助。然而,我在 linux 环境中工作,你可能需要在这里和那里调整一些东西才能让它在你的 OS X 环境中工作。上面的rosettacode.org 链接将派上用场来帮助您。
这是测试动态库的代码
[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
Run Code Online (Sandbox Code Playgroud)
编译为
[username@hostname:~/test]$gfortran -c test.f90
[username@hostname:~/test]$gfortran -shared -o test.so test.o
Run Code Online (Sandbox Code Playgroud)
这是测试程序
[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
Run Code Online (Sandbox Code Playgroud)
编译并运行为:
[username@hostname:~/test]$gfortran -o example example.f90 -ldl
[username@hostname:~/test]$./example
t_times2, i2= 30
t_square, i2= 225
[username@hostname:~/test]$
Run Code Online (Sandbox Code Playgroud)