Fortran动态库,在运行时加载? [英] Fortran dynamic libraries, load at runtime?
问题描述
运行时是否有可能让Fortran程序加载Fortran库?如果是这样,是否有可能修改一个函数并仅重新编译该库以使最初编译的程序在运行时调用库中的修改函数?
如果任何人都可以提供一个最基本的工作示例,说明如何实现这一目标将是一件好事。
帮助:
- 此页面在rosettacode.org 上,它提供了有关详细信息的完整示例并讨论了在Linux和MACOS上的实现
- intel论坛帖子 Steve Lionel给出了一些关于如何做使用ifort动态加载
- 本 IBM页面与一个动态库及其用法的很好解释
如果你想要一个简单易懂的代码,请继续阅读。几天前,我正在玩动态加载。我下面的测试代码可能对您有所帮助。但是我在linux环境下工作,你可能不得不适应一些东西在它的OS X环境中工作。上面的 rosettacode.org链接可以帮助您。
以下是测试动态库的代码
[用户名@主机名:〜/ test] $ cat test .f90
模块测试
use,intrinsic :: iso_c_binding
包含
子程序t_times2(v_in,v_out)bind(c,name ='t_times2')
整数,意图(in):: v_in
整数,意图(out):: v_out
!
v_out = v_in * 2
结束子程序t_times2
!
子程序t_square(v_in,v_out)bind(c,name ='t_square')
整数(c_int),intent(in):: v_in
整数(c_int),intent(out) :: v_out
!
v_out = v_in ** 2
结束子程序t_square
结束模块测试
编译为
[用户名@主机名:〜/ test] $ gfortran -c test.f90
[ username @ hostname:〜/ test] $ gfortran -shared -o test.so test.o
这里是测试程序
pre $ [用户名@主机名:〜/ test] $ cat example.f90
程序示例
use :: iso_c_binding
implicit none
integer(c_int),parameter :: rtld_lazy = 1! C头文件中的值extracte
integer(c_int),parameter :: rtld_now = 2! C头文件中的值extracte
!
!接口到Linux API
接口
函数dlopen(文件名,模式)bind(c,name =dlopen)
! void * dlopen(const char * filename,int mode);
使用iso_c_binding
隐式无
类型(c_ptr):: dlopen
字符(c_char),意图(in)::文件名(*)
整数(c_int) ,value :: mode
结束函数
函数dlsym(句柄,名称)bind(c,name =dlsym)
! void * dlsym(void * handle,const char * name);
使用iso_c_binding
隐式无
类型(c_funptr):: dlsym
类型(c_ptr),value :: handle
字符(c_char),intent(in): :name(*)
结束函数
函数dlclose(句柄)bind(c,name =dlclose)
! int dlclose(void * handle);
使用iso_c_binding
隐式无
整数(c_int):: dlclose
类型(c_ptr),value :: handle
结束函数
结束接口
!定义回调例程的界面。
抽象接口
子程序called_proc(i,i2)bind(c)
use,intrinsic :: iso_c_binding
整数(c_int),intent(in):: i
integer(c_int),intent(out):: i2
结束子程序called_proc
结束界面
!测试动态加载
integer i,i2
type(c_funptr):: proc_addr
type(c_ptr):: handle
字符(256):: pName,lName
过程(called_proc),绑定(c),指针:: 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 $ b $ 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 $ b $ end if
call c_f_procpointer(proc_addr,proc)
call proc(i,i2)
write(*,*)t_square,i2 =, i2
包含
结束程序示例
编译并运行为:
[username @ hostname:〜/ test] $ gfortran -o example example.f90 -ldl
[username @ hostname:〜 /test]$./example
t_times2,i2 = 30
t_square,i2 = 225
[用户名@主机名:〜/ test] $
Is it possible to have a Fortran program load a Fortran library at run time? If so, would it be possible to modify a function and recompile only the library to have the originally-compiled program call the modified function in the library at run time?
If anyone can provide a minimal working example of how this could be achieved that would be great.
Here are some few links that can be helpfull:
- This page on rosettacode.org which gives complete example with details and discuss implementation on linux and MACOS
- This intel forum post where Steve Lionel give some advice on how to do the dynamic loading with ifort
- this IBM page with a great explanation of dynamic libraries and their usage
If you want a small easy to understand code, keep reading. Few days ago, I was playing with dynamic loading. My test code below might be of help to you. However I work in the linux environment and you might have to adapt few thing here and there for it to work on your OS X environment. The rosettacode.org link above will come handy to help you.
Here is the code for the test dynamic lib
[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
Compiled as
[username@hostname:~/test]$gfortran -c test.f90
[username@hostname:~/test]$gfortran -shared -o test.so test.o
Here is the test program
[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
Compiled and run as:
[username@hostname:~/test]$gfortran -o example example.f90 -ldl
[username@hostname:~/test]$./example
t_times2, i2= 30
t_square, i2= 225
[username@hostname:~/test]$
这篇关于Fortran动态库,在运行时加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!