Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python f2py的问题:子例程中对模块的引用未定义_Python_Fortran_F2py - Fatal编程技术网

Python f2py的问题:子例程中对模块的引用未定义

Python f2py的问题:子例程中对模块的引用未定义,python,fortran,f2py,Python,Fortran,F2py,我正在尝试用f2py编译一个Fortran f90文件,以便在Python中使用。该文件是从另一个文件调用模块的子例程。该模块基本上用于分配。我可以在命令窗口中使用“gfortran my_dec.f90”编译模块,但在尝试编译子例程文件时出错。这尤其困难,因为我很少使用Fortran,这是别人的代码 以下是模块和子例程的一部分,因为它相当长,包括它的开始和结束: module my_dec integer ndir, nfreq integer ihmax,ier

我正在尝试用f2py编译一个Fortran f90文件,以便在Python中使用。该文件是从另一个文件调用模块的子例程。该模块基本上用于分配。我可以在命令窗口中使用“gfortran my_dec.f90”编译模块,但在尝试编译子例程文件时出错。这尤其困难,因为我很少使用Fortran,这是别人的代码

以下是模块和子例程的一部分,因为它相当长,包括它的开始和结束:

 module my_dec

    integer ndir, nfreq
    integer ihmax,ier
    integer nk,nth,nspec


    real hspmin
    real wsmult
    real wscut

    logical flcomb, flc 

    parameter(ndir=24)
    parameter(nfreq=23)

    parameter(nk=nfreq)
    parameter(nth=ndir)
    parameter(nspec=nk*nth)

    REAL DTH, SIG(0:nk+1), DSII(0:nk+1), DSIP(0:nk+1)
    REAL ECOS(nspec+nth), ESIN(nspec+nth), XFR 
    REAL FACHFE, TH(nth), FTE

    REAL ES2(nspec+NTH),EC2(nspec+NTH),ESC(nspec+NTH)
    REAL DDEN(NK),DDEN2(nspec)
    REAL SIG2(nspec)

    INTEGER IAPROC, NAPERR, NDSE, NDST

    INTEGER year, TIME 

    real pcg ! percentage either side of peakfor gamma estimate

    data pcg/0.3/ 

end module my_dec
子程序:

c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
xb): undefined reference to `__my_dec_MOD_iaproc'
c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
x15): undefined reference to `__my_dec_MOD_naperr'
c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
x26): undefined reference to `__my_dec_MOD_ndst'
c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
x4f): undefined reference to `__my_dec_MOD_flc'
子程序my_init

use my_dec
use constants

iaproc=1
naperr=1
ndset=1
ndst=1

IHM    = 100
HSPM   = 0.05
WSM    = 1.7
WSC    = 0.333
FLC    = .true.


IHMAX  = MAX ( 50, IHM )
HSPMIN = MAX ( 0.0001 , HSPM )
WSMULT = MAX ( 1. , WSM )
WSCUT  = MIN ( 1.0001 , MAX ( 0. , WSC ) )
FLCOMB = FLC

...      

    return

    end
当我尝试使用'f2py-c my_init.f90-m my_init_m'编译子例程文件'my_init.f90'时,我在子例程中得到一大堆关于模块变量引用的消息:

c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
xb): undefined reference to `__my_dec_MOD_iaproc'
c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
x15): undefined reference to `__my_dec_MOD_naperr'
c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
x26): undefined reference to `__my_dec_MOD_ndst'
c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
x4f): undefined reference to `__my_dec_MOD_flc'
然后是错误,这对我来说没什么启示:

collect2: ld returned 1 exit status
error: Command "C:\Python27\Scripts\gfortran.exe -Wall -Wall -shared c:\users\lw
l\appdata\local\temp\tmptlve6z\Release\users\lwl\appdata\local\temp\tmptlve6z\sr
c.win-amd64-2.7\my_init_mmodule.o c:\users\lwl\appdata\local\temp\tmptlve6z\Rele
ase\users\lwl\appdata\local\temp\tmptlve6z\src.win-amd64-2.7\fortranobject.o c:\
users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o -Lc:\python27\egg-info\
mingw\usr\lib\gcc\x86_64-w64-mingw32\4.5.2 -LC:\Python27\libs -LC:\Python27\PCbu
ild\amd64 -lpython27 -lgfortran -o .\my_init_m.pyd" failed with exit status 1
几天来,我一直在努力解决这个问题,包括在互联网上搜索,但都无济于事。有人有什么想法吗?这可能是一个相当简单的问题。谢谢你的帮助


编辑:如果我将模块复制并粘贴到与子例程相同的文件中,我就可以使用它,但是如果它作为单独的文件与它们一起使用,那就更好了

请原谅,如果这个解释涵盖了你们已经知道的领域,但你们写的是你们几乎不懂Fortran

您的例程
my_init
使用名为
my_dec
的模块(还有一个名为
constants
)。这就是
语句使用的状态。错误消息,例如

c:\users\lwl\appdata\local\temp\tmptlve6z\Release\my_init.o:my_init.f90:(.text+0
xb): undefined reference to `__my_dec_MOD_iaproc'
如果您试图编译
my_init
,而不提供要链接的
my_dec
的编译版本,则我希望看到这些。诸如
\uu my\u dec\u MOD\u iaproc
之类的名称是由编译器生成的,您可以将该名称理解为在
模块中识别名为
iaproc
的实体。如果
my_dec
根本没有定义
iaproc
,您会收到类似的消息,但这里的情况并非如此

撇开
f2py
不谈,您(在大多数情况下)只需确保由另一个模块或子程序或程序使用的
任何模块都是先编译的,链接器将发挥其魔力(提供路径设置等)

我不知道你是如何告诉
f2py
在哪里可以找到
my_dec
的编译版本的


我看到你现在在编辑中找到了解决方案。我不明白为什么您认为将子例程的源代码放在一个单独的文件中会更好。如果您真的想编写Fortran 90子例程,那么它也属于模块。

谢谢您的解释。我试着先将我的_dec编译成一个“.mod”文件,然后用f2py编译它,但没有成功。我认为您所说的是关键问题,我们不知道如何告诉f2py您在哪里查找编译版本。我并不是特别想把源代码和子程序放在分离的文件中,只是原来的代码是这样的,所以我不想改变它。如果你真的不能把子例程的源代码放进同一个文件中,你可以考虑包含它的模块的源代码,我是说,在模块文件的正确位置使用语句
包括my_init.f90
。但这确实需要修改代码。