Compile Fortran module with f2py(使用 f2py 编译 Fortran 模块)
问题描述
我有一个 Fortran 模块,我正在尝试使用 f2py(如下所列)进行编译.当我删除模块声明并将子例程单独留在文件中时,一切正常.但是,如果模块声明如下所示,我会得到以下结果:
I have a Fortran module which I am trying to compile with f2py (listed below). When I remove the module declaration and leave the subroutine in the file by itself, everything works fine. However, if the module is declared as shown below, I get the following results:
> f2py.py -c -m its --compiler=mingw itimes-s2.f
...
Reading fortran codes...
Reading file 'itimes-s2.f' (format:fix,strict)
crackline: groupcounter=1 groupname={0: '', 1: 'module', 2: 'interface', 3: 'subroutine'}
crackline: Mismatch of blocks encountered. Trying to fix it by assuming "end" statement.
...
c:usersastay13appdatalocal emp mpgh5ag8Releaseusersastay13appdatalocal emp mpgh5ag8src.win32-3.2itsmodule.o:itsmodule.c:(.data+0xec): undefined reference to `itimes_'
collect2: ld returned 1 exit status
在 f2py 中编译模块或子程序有什么不同?我是否在模块中遗漏了导致 f2py 出现问题的重要内容?请注意,当我单独使用 gfortran 时,模块编译得很好.
What is different about compiling a module or subroutine in f2py? Have I left something important out in the module that causes f2py to have trouble? Note that the module compiles fine when I use gfortran alone.
软件:Windows 7;gcc,gfortran 4.6.1(MinGW);蟒蛇3.2.2;f2py v2
Software: Windows 7; gcc, gfortran 4.6.1 (MinGW); python 3.2.2; f2py v2
times-s2.f:
itimes-s2.f:
module its
contains
subroutine itimes(infile,outfile)
implicit none
! Constants
integer, parameter :: dp = selected_real_kind(15)
! Subroutine Inputs
character(*), intent(in) :: infile
character(*), intent(in) :: outfile
! Internal variables
real(dp) :: num
integer :: inu
integer :: outu
integer :: ios
inu = 11
outu = 22
open(inu,file=infile,action='read')
open(outu,file=outfile,action='write',access='append')
do
read(inu,*,IOSTAT=ios) num
if (ios < 0) exit
write(outu,*) num**2
end do
end subroutine itimes
end module its
推荐答案
您试图在 Python 模块中包含 Fortran 模块.如果需要,名称必须不同,例如
You are trying to have a Fortran module in a Python module. If you want that, the names must be different, e.g.
f2py.py -c -m SOMEDIFFERENTNAME itimes-s2.f
结果将被称为 pythonmodule.fortranmodule.yourfunction()
.
你也可以导入为
from pythonmodule import fortranmodule
fortranmodule.yourfunction()
否则它在我的机器上工作.
Otherwise it worked on my machine.
这篇关于使用 f2py 编译 Fortran 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 f2py 编译 Fortran 模块
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01