[英]How to pass a parameter of type external from C to Fortran
假設我在Fortran中有以下子例程
subroutine exec(routine) implicit none external :: routine real(kind=8) :: res call routine(2.0d0, res) print *, "Fortran Result: res = ", res end subroutine exec
該子例程接收一個外部例程作為參數。 現在,假設此例程是用C編寫的,我還需要從C調用Fortran例程exec 。 像這樣:
void op(double x, double *f) { *f = pow(x, 2); } void main() { exec_(op); }
我知道,如果我不是傳遞外部子例程,而是傳遞整數,雙精度數或其他常規類型,它將起作用,但是此代碼返回分段錯誤。 有什么方法可以將C 外部類型的參數傳遞給Fortran?
這有效,您必須使用指針:
void op(double *x, double *f) {
*f = pow(*x, 2);
}
void main() {
exec_(&op);
}
subroutine exec(routine)
implicit none
external :: routine
real(kind(1.d0)) :: res
call routine(2.0d0, res)
print *, "Fortran Result: res = ", res
end subroutine exec
不要用kind=8
不申報8個字節的真實,而是一種依賴於處理器類型與種類數8連老和非標准real*8
會更好。
但是最好使用Fortran 2003 c互操作性和iso_c_binding
模塊:
f.f90
子例程exec(routine_c)bind(C,name =“ exec_”)使用iso_c_binding隱式無
abstract interface subroutine sub(a,b) bind(C) use iso_c_binding implicit none real(c_double),value :: a real(c_double) :: b end subroutine end interface type(c_funptr),value :: routine_c real(c_double) :: res procedure(sub),pointer :: routine call c_f_procpointer(routine_c,routine) call routine(2.0d0, res) print *, "Fortran Result: res = ", res
結束子程序執行
抄送
#include <math.h>
void op(double x, double *f) {
*f = pow(x, 2.0);
}
int main() {
extern void exec_(void (*) (double, double *));
exec_(&op);
return 0;
}
匯編:
gfortran c.c f.f90 -Wall -g -fbacktrace -fcheck=all
cc1: warning: command line option ‘-fbacktrace’ is valid for Fortran but not for C [enabled by default]
cc1: warning: command line option ‘-fcheck=all’ is valid for Fortran but not for C [enabled by default]
./a.out
Fortran Result: res = 4.0000000000000000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.