【发布时间】: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 中的所有工具,但除非您完全了解您在做什么,否则不要尝试混合使用这两种工具.