Problems with f2py: undefined references to module in subroutine

I am trying to compile a Fortran f90 file with f2py, to use in Python. The file is a subroutine that calls a module from another file. The module is basically for allocation. I can compile the module, using 'gfortran my_dec.f90' in the command window, but I get errors when trying to compile the subroutine file. It's especially difficult because I've barely used Fortran and this is someone else's code.

Here are the module and a section of the subroutine, because it's quite long, including its start and end:

 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

subroutine:

subroutine 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

When I try to compile the subroutine file, 'my_init.f90', using 'f2py -c my_init.f90 -m my_init_m' I get a whole bunch of messages, about references to variables from the module, in the subroutine:

c:userslwlappdatalocaltemptmptlve6zReleasemy_init.o:my_init.f90:(.text+0
xb): undefined reference to `__my_dec_MOD_iaproc'
c:userslwlappdatalocaltemptmptlve6zReleasemy_init.o:my_init.f90:(.text+0
x15): undefined reference to `__my_dec_MOD_naperr'
c:userslwlappdatalocaltemptmptlve6zReleasemy_init.o:my_init.f90:(.text+0
x26): undefined reference to `__my_dec_MOD_ndst'
c:userslwlappdatalocaltemptmptlve6zReleasemy_init.o:my_init.f90:(.text+0
x4f): undefined reference to `__my_dec_MOD_flc'

and then the error, which doesn't reveal much to me:

collect2: ld returned 1 exit status
error: Command "C:Python27Scriptsgfortran.exe -Wall -Wall -shared c:userslw
lappdatalocaltemptmptlve6zReleaseuserslwlappdatalocaltemptmptlve6zsr
c.win-amd64-2.7my_init_mmodule.o c:userslwlappdatalocaltemptmptlve6zRele
aseuserslwlappdatalocaltemptmptlve6zsrc.win-amd64-2.7fortranobject.o c:
userslwlappdatalocaltemptmptlve6zReleasemy_init.o -Lc:python27egg-info
mingwusrlibgccx86_64-w64-mingw324.5.2 -LC:Python27libs -LC:Python27PCbu
ildamd64 -lpython27 -lgfortran -o .my_init_m.pyd" failed with exit status 1

I've been trying to work this out for a couple of days, including searching the internet, but to no avail. Anyone have any ideas? it could be quite a simple problem. Thanks for any help.

Edit: I've got it to work if I copy and paste the module into the same file as the subroutine, but it would be nice to have it work with them as seperate files.


Excuse me if this explanation covers ground you already know, but you do write that you barely know Fortran.

Your routine my_init uses the module called my_dec (and one called constants ). That's what the use statements state. The error messages such as

c:userslwlappdatalocaltemptmptlve6zReleasemy_init.o:my_init.f90:(.text+0
xb): undefined reference to `__my_dec_MOD_iaproc'

are what I would expect to see if you attempted to compile my_init without providing the compiled version of my_dec to link to. Names such as __my_dec_MOD_iaproc are generated by the compiler, you could read that name as identifying an entity called iaproc in the MODule my_dec . You'd get a similar message if my_dec did not define iaproc at all, but that's not the case here.

Leaving aside f2py you (in most cases) simply have to ensure that any module which is used by another module or subprogram or program is compiled first, the linker will do its magic (provided paths are set and so forth).

I don't know how you tell f2py where to look for the compiled version of my_dec .

And I see that you have now, in your edit, figured out the solution. I'm puzzled as to why you think it would be nice to have the source for the subroutine in a separate file. If you really are trying to write Fortran 90 subroutines belong in modules too.

链接地址: http://www.djcxy.com/p/96134.html

上一篇: f2py不喜欢子程序中的显式整形数组

下一篇: f2py的问题:未定义的子程序中的模块引用