使用 f2py 编译 Fortran 模块
我有一个 Fortran 模块,我正在尝试使用 f2py 进行编译(如下所列)。当我删除模块声明并将子例程单独保留在文件中时,一切正常。但是,如果模块声明如下所示,我会得到以下结果:
> 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:\users\astay13\appdata\local\temp\tmpgh5ag8\Release\users\astay13\appdata\local\temp\tmpgh5ag8\src.win32-3.2\itsmodule.o:itsmodule.c:(.data+0xec): undefined reference to `itimes_'
collect2: ld returned 1 exit status
在 f2py 中编译模块或子例程有什么不同?我是否在模块中遗漏了一些导致 f2py 出现问题的重要内容?请注意,当我单独使用 gfortran 时,该模块可以正常编译。
软件:Windows 7; gcc、gfortran 4.6.1 (MinGW);蟒蛇3.2.2; f2py v2
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
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:\users\astay13\appdata\local\temp\tmpgh5ag8\Release\users\astay13\appdata\local\temp\tmpgh5ag8\src.win32-3.2\itsmodule.o:itsmodule.c:(.data+0xec): undefined reference to `itimes_'
collect2: ld returned 1 exit status
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.
Software: Windows 7; gcc, gfortran 4.6.1 (MinGW); python 3.2.2; f2py v2
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试在 Python 模块中包含 Fortran 模块。如果你想要这样,名称必须不同,例如
结果将被称为
pythonmodule.fortranmodule.yourfunction()
。您也可以将其导入,
否则它可以在我的机器上运行。
You are trying to have a Fortran module in a Python module. If you want that, the names must be different, e.g.
The result will be called as
pythonmodule.fortranmodule.yourfunction()
.You can also import it as
Otherwise it worked on my machine.
为了使 f2py 工作,您需要有一个签名文件来指导界面创建或使用 f2py 注释修改源代码以帮助界面。请参阅 https://numpy.org/doc/stable/f2py/signature-file .html 了解更多信息。
从该站点:
现在可以通过一个命令来构建扩展模块:
For f2py to work you need to have a signature file to direct the interface creation or modify your source code with f2py comments to help with the interface. See https://numpy.org/doc/stable/f2py/signature-file.html for more information.
From that site:
Building the extension module can be now carried out in one command:
要使用 f2py 包装 F90 模块,您需要指定可以从模块外部访问哪些功能。为此,您有 private 和 public fortran90 关键字。您还应该将扩展名从 .f 更改为 .F90 或 .f90。我对您的代码进行了一些更改,以演示
public
和private
关键字的使用。首先创建包含模块对象的 Fortran 接口的签名文件:
然后编译模块:
您的代码已修改并命名为 module_its.f90:
To wrap a F90 module with f2py you do need to specify what features can be accessible from outside the module. For that you have the private and public fortran90 keywords. You should also change the extension from .f to .F90 or .f90. I made some changes in your code that demonstrates the use of
public
andprivate
keywords.First of all create the signature file that hols the fortran interfaces for the module objects:
THen compile the module:
Your code modified and named as module_its.f90: