【发布时间】:2016-11-23 11:53:10
【问题描述】:
我花了几天时间学习 Fortran 的 C 互操作性,以便从 C 调用 Fortran 函数 DLL。 在这里我找到了这个链接: Use Fortran-code in C
我尝试像这样创建一个 Fortran DLL,而我的编译器是 Intel Fortran 编译器:
module integration
implicit none
contains
function Integrate(func, a,b, intsteps) result(integral)
!DEC$ ATTRIBUTES DLLEXPORT :: Integrate
interface
real function func(x)
real, intent(in) :: x
end function func
end interface
real :: integral, a, b
integer :: intsteps
intent(in) :: a, b, intsteps
optional :: intsteps
real :: x, dx
integer :: i,n
integer, parameter :: rk = kind(x)
n = 1000
if (present(intsteps)) n = intsteps
dx = (b-a)/n
integral = 0.0_rk
do i = 1,n
x = a + (1.0_rk * i - 0.5_rk) * dx
integral = integral + func(x)
end do
integral = integral * dx
end function
end module integration
real(c_float) function wrapper_integrate(func, a, b, intsteps) result(integral) bind(C, name='integrate')
use iso_c_binding
use integration
interface
function iFunc(x) bind(C)
use, intrinsic :: iso_c_binding
real(c_float) :: iFunc
real(c_float), intent(in) :: x
end function iFunc
end interface
type(C_FUNPTR), INTENT(IN), VALUE :: func
real(c_float) :: a,b
integer(c_int),intent(in) :: intsteps
optional :: intsteps
procedure(iFunc),pointer :: myfunc
call c_f_procpointer(func, myfunc)
if (present(intsteps)) then
integral = Integrate(myfunc,a,b,intsteps) <==error #8128
else
integral = Integrate(myfunc,a,b) <==error #8128
endif
end function wrapper_integrate
当我构建 DLL 时,错误显示如下
error #8128: The BIND attribute of the associated actual procedure differs from the BIND attribute of the dummy procedure. [MYFUNC]
代码中表示的错误行,看起来myfunc 不等于模块集成中定义的func。也许我可以使用iso_c_binding 来修改模块integration。
有时某些 Fortrans 函数有许多参数要在函数中分配和使用,在 Fortran 中使用 iso_c_binding 并不容易。那么如何解决MYFUNC中的错误呢?
【问题讨论】:
-
Integrate中的虚拟程序具有bind属性;函数指针myfunc是实际参数,它具有给定的接口iFunc,它没有bind接口。 -
是的!我忘了!现在我可以导出 Fortran Dll。
-
感谢您编辑我的问题,使它变得整洁!
标签: visual-c++ fortran intel-fortran fortran-iso-c-binding