|
首先是pasuka 大侠的例子fortran 源码:
[Fortran] 纯文本查看 复制代码 module py2f90
use,intrinsic::iso_c_binding
implicit none
contains
subroutine transferMat2For(matrix,n1,n2)bind(c,name='array2py')
implicit none
integer(c_int),intent(in),value::n1,n2
real(c_float),intent(out)::matrix(n1,n2)
integer::i,j
! initialize matrix
matrix = 0.0E0
! loop
do i=1,n1
do j=1,n2
matrix(i,j) = real(i,4)*1.E1+real(j,4)*2.E0
write(*,"('Row:',i4,1x,'Col:',i4,1x,'Value:',1x,F5.2)")i,j,matrix(i,j)
enddo
enddo
return
end subroutine
end module
python 源码:
[Fortran] 纯文本查看 复制代码 import numpy as np
from numpy.ctypeslib import load_library,ndpointer
from ctypes import c_int
# shape of 2d array
n1,n2 = 2,4+1
# create an empty 2d array
data = np.empty(shape=(n1,n2),dtype='f4',order='f')
flib = load_library("mydll.dll","./")
flib.argtypes = [ndpointer(dtype='f4',ndim=2),c_int,c_int]
flib.array2py(data.ctypes.data,n1,n2)
python 编译运行,提示Traceback (most recent call last): File "E:/try/python/t1/t1.py", line 12, in <module> flib.array2py(data.ctypes.data,n1,n2) File "F:\software\anaconda\lib\ctypes\__init__.py", line 369, in __getattr__ func = self.__getitem__(name) File "F:\software\anaconda\lib\ctypes\__init__.py", line 374, in __getitem__ func = self._FuncPtr((name_or_ordinal, self))AttributeError: function 'array2py' not found
啥情况啊?我的python 版本3.73,64位,intel visual fortran 64位。。。。谢谢大侠指点。
|
|