【问题标题】:F2PY gives unresolved externals error with PRINTF2PY 使用 PRINT 给出未解决的外部错误
【发布时间】:2018-08-14 23:45:22
【问题描述】:

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

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 位)。我想我正在为 Python 使用 Visual C++。我的操作系统是 Windows 10。

感谢您的帮助!

【问题讨论】:

  • 我没有在您使用的任何工具中看到 Cygwin。该错误是由于缺少与 Fortran 共享库的链接。你的编译器有 Fortran 导入库吗?
  • FORTRAN 编译器作为 Cygwin 的一部分安装,此处为:C:\cygwin64\bin\gfortran.exe。但我不知道如何检查我的编译器是否有 Fortran 导入库。我会在哪个文件夹中找到它?对不起,我不是很了解
  • 有两个文件,libgfortran.dll.a 和 libgfortran.a,位于文件夹 C:\cygwin64\lib\gcc\x86_64-pc-cygwin\6.4.0 中。我认为我们正在链接到该文件夹​​,对吧?
  • error LNK2019 不是来自 cygwin 链接器,因此您正在混合 cygwin 而不是 cygwin 工具。它永远不会起作用。如果你想尝试 Cygwin,你需要使用 Cygwin python 作为开始。
  • 确实,看起来您正在使用来自 Anaconda 的本机 Windows Python。 Conda 也将 gfortran 打包,因此您应该使用 conda 环境中提供的所有工具,或者使用 Cygwin 中的所有工具,但除非您完全了解您在做什么,否则不要尝试混合使用这两种工具.

标签: gcc cygwin gfortran f2py


【解决方案1】:

根据上面的 cmets,我找到了适合我的解决方案。

  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 2015-01-11
    • 2014-02-02
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多