【发布时间】:2015-05-24 18:08:07
【问题描述】:
我尝试在 C 中使用 fortran 例程,但我不工作。我不知道我在哪里做错了。这是我的 Fortran 代码,包括我想在 C 中使用的集成模块:
module integration
implicit none
contains
function Integrate(func, a,b, intsteps) result(integral)
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
real(c_float) function func(x) bind(C)
use, intrinsic :: iso_c_binding
real(c_float), intent(in) :: x
end function func
end interface
real(c_float) :: a,b
integer(c_int),intent(in) :: intsteps
optional :: intsteps
if (present(intsteps)) then
integral = Integrate(func,a,b,intsteps)
else
integral = Integrate(func,a,b)
endif
end function wrapper_integrate
还有我的 C 代码:
#include <stdio.h>
#include <math.h>
float sin2(float x) {
return sin(x) * sin(x);
}
float integrate(float(*func)(float), float a, float b, int intsteps);
int main() {
float integral;
integral = integrate(sin2,0.,1.,10000);
printf("%f",integral);
return 0;
}
如果我执行
g++ -c main.c
gfortran -c integration.f95
g++ main.o integration.o
我明白了
undefined reference to `integrate(float (*)(float), float, float, int)'
有人知道怎么处理吗?
【问题讨论】:
-
感谢您的链接,但我不知道如何使用它。如果我将 func 的类型定义为 C_FUNPTR 它仍然不起作用。你知道我的例子中的 func 必须是什么吗?
-
FORTRAN 期望什么调用约定?
标签: c fortran fortran-iso-c-binding