Fortran程序调用

来自Shiyin's note
159.226.169.140讨论2012年4月7日 (六) 11:19的版本 (以“先要给要调用的Fortran程序写一个接口,例 接口程序: SUBROUTINE SUM_ARRAY(argc, argv) !Called by IDL INTEGER*4 argc, argv(*) !Argc ...”为内容创建页面)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

先要给要调用的Fortran程序写一个接口,例

接口程序:

SUBROUTINE SUM_ARRAY(argc, argv)  !Called by IDL
INTEGER*4 argc, argv(*)           !Argc and Argv are integers
j = LOC(argc)         !Obtains the number of arguments (argc)
                     !Because argc is passed by VALUE.
;
! Call subroutine SUM_ARRAY1, converting the IDL parameters
! to standard Fortran, passed by reference arguments:
CALL SUM_ARRAY1(%VAL(argv(1)), %VAL(argv(2)), %VAL(argv(3)))
RETURN
END

调用程序:

! This subroutine is called by SUM_ARRAY and has no  IDL specific code.
SUBROUTINE SUM_ARRAY1(array, n, sum)
INTEGER*4 n
REAL*4 array(n), sum
sum=0.0
DO i=1,n
sum = sum + array(i)
ENDDO
RETURN
END

编译生成动态链接库文件

ifort -shared -fpic  SUM_ARRAY.f SUM_ARRAY1.f -o example1.so
-shared   produce a shared object
-fpic  generate position independent code (-fno-pic/-fno-PIC is DEFAULT)

然后在IDL中的调用方法如下

X = FINDGEN(10) ; Make an array.
sum = 0.0
S = CALL_EXTERNAL('example1.so', 'sum_array_', X, N_ELEMENTS(X), sum)