F2PY通过PRINT给出未解决的外部错误

问题描述 投票:0回答:1

我有一些FORTRAN代码,我想用F2PY编译。代码如下:

C FILE: FTYPE.F
      SUBROUTINE FOO(N)
      INTEGER N
Cf2py integer optional,intent(in) :: n = 13
      REAL A,X
      COMMON /DATA/ A,X(3)
      PRINT*, "abc"
      END
C END OF FTYPE.F

当我从Powershell运行以下命令时:

f2py -c .\ftype.f -m ftype --compiler=msvc

我收到以下错误:

ftype.o : error LNK2019: unresolved external symbol _gfortran_st_write referenced in function foo_
ftype.o : error LNK2019: unresolved external symbol _gfortran_transfer_character_write referenced in function foo_
ftype.o : error LNK2019: unresolved external symbol _gfortran_st_write_done referenced in function foo_
.\ftype.pyd : fatal error LNK1120: 3 unresolved externals

基于我在这里看到的其他答案,我觉得我可能没有链接到所需的库。这是整个输出:

PS C:\Users\Brian\Desktop\F2PY> f2py -c .\ftype.f -m ftype --compiler=msvc

C:\Users\Brian\Desktop\F2PY>call "C:\Anaconda2\Scripts\\..\python.exe" "C:\Anaconda2\Scripts\\f2py.py" -c .\ftype.f -m ftype --compiler=msvc
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
  commands have different --compiler options: ['mingw32', 'msvc'], using first in list as default
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "ftype" sources
f2py options: []
f2py:> c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftypemodule.c
creating c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7
Reading fortran codes...
        Reading file '.\\ftype.f' (format:fix,strict)
Post-processing...
        Block: ftype
                        Block: foo
Post-processing (stage 2)...
Building modules...
        Building module "ftype"...
                Constructing wrapper function "foo"...
                  foo([n])
                Constructing COMMON block support for "data"...
                  a,x
        Wrote C/API module "ftype" to file "c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftypemodule.c"
        Fortran 77 wrappers are saved to "c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype-f2pywrappers.f"
  adding 'c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\fortranobject.c' to sources.
  adding 'c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7' to include_dirs.
copying C:\Anaconda2\lib\site-packages\numpy\f2py\src\fortranobject.c -> c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7
copying C:\Anaconda2\lib\site-packages\numpy\f2py\src\fortranobject.h -> c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7
  adding 'c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype-f2pywrappers.f' to sources.
build_src: building npy-pkg config files
running build_ext
customize MSVCCompiler
customize MSVCCompiler using build_ext
customize GnuFCompiler
Could not locate executable g77
Could not locate executable f77
customize IntelVisualFCompiler
Found executable C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2018.3.210\windows\bin\intel64\ifort.exe
customize AbsoftFCompiler
Could not locate executable f90
customize CompaqVisualFCompiler
Found executable C:\cygwin64\bin\DF.exe
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Found executable C:\cygwin64\bin\gfortran.exe
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'ftype' extension
compiling C sources
creating c:\users\brian\appdata\local\temp\tmpra_1ng\Release
creating c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users
creating c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian
creating c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata
creating c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local
creating c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp
creating c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng
creating c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7
C:\Users\Brian\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Ic:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7 -IC:\Anaconda2\lib\site-packages\numpy\core\include -IC:\Anaconda2\include -IC:\Anaconda2\PC /Tcc:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftypemodule.c /Foc:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftypemodule.obj
C:\Users\Brian\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Ic:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7 -IC:\Anaconda2\lib\site-packages\numpy\core\include -IC:\Anaconda2\include -IC:\Anaconda2\PC /Tcc:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\fortranobject.c /Foc:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\fortranobject.obj
compiling Fortran sources
Fortran f77 compiler: C:\cygwin64\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -O0
Fortran f90 compiler: C:\cygwin64\bin\gfortran.exe -Wall -g -fno-second-underscore -O0
Fortran fix compiler: C:\cygwin64\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -O0
compile options: '-Ic:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7 -IC:\Anaconda2\lib\site-packages\numpy\core\include -IC:\Anaconda2\include -IC:\Anaconda2\PC -c'
gfortran.exe:f77: .\ftype.f
gfortran.exe:f77: c:\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype-f2pywrappers.f
C:\Users\Brian\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\cygwin64\lib\gcc\x86_64-pc-cygwin\6.4.0 /LIBPATH:C:\Anaconda2\libs /LIBPATH:C:\Anaconda2\PCbuild\amd64 /LIBPATH:C:\Anaconda2\PC\VS9.0\amd64 /LIBPATH:C:\Anaconda2\libs /LIBPATH:C:\Anaconda2\PCbuild\amd64 /LIBPATH:C:\Anaconda2\PC\VS9.0\amd64 /EXPORT:initftype c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftypemodule.obj c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\fortranobject.obj c:\users\brian\appdata\local\temp\tmpra_1ng\Release\ftype.o c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype-f2pywrappers.o /OUT:.\ftype.pyd /IMPLIB:c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype.lib /MANIFESTFILE:c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype.pyd.manifest
ftypemodule.obj : warning LNK4197: export 'initftype' specified multiple times; using first specification
   Creating library c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype.lib and object c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype.exp
ftype.o : error LNK2019: unresolved external symbol _gfortran_st_write referenced in function foo_
ftype.o : error LNK2019: unresolved external symbol _gfortran_transfer_character_write referenced in function foo_
ftype.o : error LNK2019: unresolved external symbol _gfortran_st_write_done referenced in function foo_
.\ftype.pyd : fatal error LNK1120: 3 unresolved externals
error: Command "C:\Users\Brian\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\cygwin64\lib\gcc\x86_64-pc-cygwin\6.4.0 /LIBPATH:C:\Anaconda2\libs /LIBPATH:C:\Anaconda2\PCbuild\amd64 /LIBPATH:C:\Anaconda2\PC\VS9.0\amd64 /LIBPATH:C:\Anaconda2\libs /LIBPATH:C:\Anaconda2\PCbuild\amd64 /LIBPATH:C:\Anaconda2\PC\VS9.0\amd64 /EXPORT:initftype c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftypemodule.obj c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\fortranobject.obj c:\users\brian\appdata\local\temp\tmpra_1ng\Release\ftype.o c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype-f2pywrappers.o /OUT:.\ftype.pyd /IMPLIB:c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype.lib /MANIFESTFILE:c:\users\brian\appdata\local\temp\tmpra_1ng\Release\users\brian\appdata\local\temp\tmpra_1ng\src.win-amd64-2.7\ftype.pyd.manifest" failed with exit status 1120

当我在FORTRAN代码中注释PRINT语句时,没有错误,代码编译。

我正在使用GNU Fortran(GCC)6.4.0。我使用Python 2.7.14 :: Anaconda自定义(64位)。我想我正在使用Visual C ++ for Python。我的操作系统是Windows 10。

谢谢您的帮助!

gcc cygwin gfortran f2py
1个回答
0
投票

根据上面的评论,我找到了一个适合我的解决方案。

  1. 我使用Windows的以下说明和设置安装了MinGW f2py import Fortran code in Python
  2. 我在PowerShell中运行以下命令: f2py -c ftype.f -m ftype --compiler = mingw32
  3. 我在python中运行以下命令: 导入ftype ftype.foo(5)
  4. 我得到以下输出: ABC

如果你很好奇,下面是整个痕迹。总的来说,我认为拥有一个地方的所有工具(MinGW)可能会有所帮助,就像上面Iguananaut的评论一样(谢谢!)。

谢谢大家的帮助!

PS C:\Users\Brian\Desktop\F2PY> f2py -c ftype.f -m ftype --compiler=mingw32

C:\Users\Brian\Desktop\F2PY>call "C:\Anaconda2\Scripts\\..\python.exe" "C:\Anaconda2\Scripts\\f2py.py" -c ftype.f -m ftype --compiler=mingw32
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "ftype" sources
f2py options: []
f2py:> c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftypemodule.c
creating c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7
Reading fortran codes...
        Reading file 'ftype.f' (format:fix,strict)
Post-processing...
        Block: ftype
                        Block: foo
Post-processing (stage 2)...
Building modules...
        Building module "ftype"...
                Constructing wrapper function "foo"...
                  foo([n])
                Constructing COMMON block support for "data"...
                  a,x
        Wrote C/API module "ftype" to file "c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftypemodule.c"
        Fortran 77 wrappers are saved to "c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftype-f2pywrappers.f"
  adding 'c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\fortranobject.c' to sources.
  adding 'c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7' to include_dirs.
copying C:\Anaconda2\lib\site-packages\numpy\f2py\src\fortranobject.c -> c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7
copying C:\Anaconda2\lib\site-packages\numpy\f2py\src\fortranobject.h -> c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7
  adding 'c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftype-f2pywrappers.f' to sources.
build_src: building npy-pkg config files
running build_ext
Cannot build msvcr library: "msvcr90d.dll" not found
customize Mingw32CCompiler
customize Mingw32CCompiler using build_ext
customize GnuFCompiler
Could not locate executable g77
Could not locate executable f77
customize IntelVisualFCompiler
Could not locate executable ifort
Could not locate executable ifl
customize AbsoftFCompiler
Could not locate executable f90
customize CompaqVisualFCompiler
Found executable C:\cygwin64\bin\DF.exe
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Found executable C:\mingw\mingw64\bin\gfortran.exe
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'ftype' extension
compiling C sources
C compiler: gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall -Wstrict-prototypes

creating c:\users\brian\appdata\local\temp\tmpt0zwdl\Release
creating c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users
creating c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian
creating c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata
creating c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local
creating c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local\temp
creating c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local\temp\tmpt0zwdl
creating c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7
compile options: '-DNPY_MINGW_USE_CUSTOM_MSVCR -D__MSVCRT_VERSION__=0x1500 -Ic:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7 -IC:\Anaconda2\lib\site-packages\numpy\core\include -IC:\Anaconda2\include -IC:\Anaconda2\PC -c'
gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall -Wstrict-prototypes -DNPY_MINGW_USE_CUSTOM_MSVCR -D__MSVCRT_VERSION__=0x1500 -Ic:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7 -IC:\Anaconda2\lib\site-packages\numpy\core\include -IC:\Anaconda2\include -IC:\Anaconda2\PC -c c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftypemodule.c -o c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftypemodule.o
gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall -Wstrict-prototypes -DNPY_MINGW_USE_CUSTOM_MSVCR -D__MSVCRT_VERSION__=0x1500 -Ic:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7 -IC:\Anaconda2\lib\site-packages\numpy\core\include -IC:\Anaconda2\include -IC:\Anaconda2\PC -c c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\fortranobject.c -o c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\fortranobject.o
compiling Fortran sources
Fortran f77 compiler: C:\mingw\mingw64\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -O0
Fortran f90 compiler: C:\mingw\mingw64\bin\gfortran.exe -Wall -g -fno-second-underscore -O0
Fortran fix compiler: C:\mingw\mingw64\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -O0
compile options: '-Ic:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7 -IC:\Anaconda2\lib\site-packages\numpy\core\include -IC:\Anaconda2\include -IC:\Anaconda2\PC -c'
gfortran.exe:f77: ftype.f
gfortran.exe:f77: c:\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftype-f2pywrappers.f
C:\mingw\mingw64\bin\gfortran.exe -Wall -g -Wall -g -shared c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftypemodule.o c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\fortranobject.o c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\ftype.o c:\users\brian\appdata\local\temp\tmpt0zwdl\Release\users\brian\appdata\local\temp\tmpt0zwdl\src.win-amd64-2.7\ftype-f2pywrappers.o -LC:\mingw\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0 -LC:\Anaconda2\libs -LC:\Anaconda2\PCbuild\amd64 -LC:\Anaconda2\PC\VS9.0\amd64 -lpython27 -lgfortran -o .\ftype.pyd
Removing build directory c:\users\brian\appdata\local\temp\tmpt0zwdl
PS C:\Users\Brian\Desktop\F2PY> python
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ftype
>>> ftype.foo(5)
 abc
© www.soinside.com 2019 - 2024. All rights reserved.