F2PY:模块中对子例程的未定义引用

2024-05-14 17:14:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用f2py为我的Fortran代码生成Python接口。我编写了一个测试模块和一个子例程来说明我的问题

testmod.f90的内容

module testmod

implicit none
public 
contains

subroutine sub_in_mod()
    print *, "Printed from subroutine in module"
end subroutine

end module 

testsub.f90的内容

subroutine subroutine_py()
    use testmod
    call sub_in_mod()
end subroutine

其目的是从Python调用subroutine_py()。根据我在网上找到的说明,我运行了以下命令

gfortran -c -fPIC testmod.f90 -o testmod.o
gfortran -c -fPIC testsub.f90 -o testsub.o
f2py -c testsub.f90 testmod.o testsub.o

但是我在最后一个命令中得到了错误testsub.f90:3: undefined reference to '__testmod_MOD_sub_in_mod'

使用f2py -c testsub.f90 testmod.f90可以成功构建,但由于复杂性和自定义类型,这种方法不适用于我的项目。所以,我想知道如何告诉f2py使用gfortran生成的对象文件。我在stackoverflow上发现了类似的问题,但都没有直接解决这个问题

编辑

根据Pierre de Buyl的评论,我清理了文件并运行了

gfortran -c -fPIC testmod.f90 -o testmod.o

f2py -c testsub.f90 testmod.o

这仍然会导致错误

这是完整的日志

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 "untitled" sources
f2py options: []
f2py:> C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8\untitledmodule.c
creating C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8
Reading fortran codes...
        Reading file 'testsub.f90' (format:free)
Post-processing...
        Block: untitled
                        Block: subroutine_py
In: :untitled:testsub.f90:subroutine_py
get_useparameters: no module testmod info used by subroutine_py
Post-processing (stage 2)...
Building modules...
        Building module "untitled"...
                Constructing wrapper function "subroutine_py"...
                  subroutine_py()
        Wrote C/API module "untitled" to file "C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8\untitledmodule.c"
  adding 'C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8\fortranobject.c' to sources.
  adding 'C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8' to include_dirs.
copying c:\users\thein lin aung\appdata\local\programs\python\python38\lib\site-packages\numpy\f2py\src\fortranobject.c -> C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8
copying c:\users\thein lin aung\appdata\local\programs\python\python38\lib\site-packages\numpy\f2py\src\fortranobject.h -> C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8
build_src: building npy-pkg config files
running build_ext
No module named 'numpy.distutils._msvccompiler' in numpy.distutils; trying from distutils
customize MSVCCompiler
customize MSVCCompiler using build_ext
get_default_fcompiler: matching types: '['gnu', 'intelv', 'absoft', 'compaqv', 'intelev', 'gnu95', 'g95', 'intelvem', 'intelem', 'flang']'
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
Could not locate executable DF
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Found executable C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe
Using built-in specs.
COLLECT_GCC=C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe
COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
customize Gnu95FCompiler
Using built-in specs.
COLLECT_GCC=C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe
COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
customize Gnu95FCompiler using build_ext
building 'untitled' extension
compiling C sources
creating C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users
creating C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users\THEINL~1
creating C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users\THEINL~1\AppData
creating C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users\THEINL~1\AppData\Local
creating C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users\THEINL~1\AppData\Local\Temp
creating C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt
creating C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8 -Ic:\users\thein lin aung\appdata\local\programs\python\python38\lib\site-packages\numpy\core\include -Ic:\users\thein lin aung\appdata\local\programs\python\python38\include -Ic:\users\thein lin aung\appdata\local\programs\python\python38\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.24.28314\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt -IC:\Users\Thein Lin Aung\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\include /TcC:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8\untitledmodule.c /FoC:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8\untitledmodule.obj
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8 -Ic:\users\thein lin aung\appdata\local\programs\python\python38\lib\site-packages\numpy\core\include -Ic:\users\thein lin aung\appdata\local\programs\python\python38\include -Ic:\users\thein lin aung\appdata\local\programs\python\python38\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.24.28314\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt -IC:\Users\Thein Lin Aung\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\include /TcC:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8\fortranobject.c /FoC:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8\fortranobject.obj
compiling Fortran sources
Fortran f77 compiler: C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -O3 -funroll-loops
Fortran f90 compiler: C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe -Wall -g -fno-second-underscore -O3 -funroll-loops
Fortran fix compiler: C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -O3 -funroll-loops
compile options: '-IC:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\src.win-amd64-3.8 -Ic:\users\thein lin aung\appdata\local\programs\python\python38\lib\site-packages\numpy\core\include -Ic:\users\thein lin aung\appdata\local\programs\python\python38\include -Ic:\users\thein lin aung\appdata\local\programs\python\python38\include -c'
Program:f90: testsub.f90
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe -Wall -g -Wall -g -shared ..\..\..\..\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\testsub.o -LC:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0 -Lc:\users\thein lin aung\appdata\local\programs\python\python38\libs -Lc:\users\thein lin aung\appdata\local\programs\python\python38\PCbuild\amd64 -o C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\.libs\libtestsub.APU2XWIZSCE75AFY5M5MJDAHS5PXOMHM.gfortran-win_amd64.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\libtestsub.APU2XWIZSCE75AFY5M5MJDAHS5PXOMHM.gfortran-win_amd64.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64
..\..\..\..\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\testsub.o: In function `subroutine_py_':
C:\Users\Thein Lin Aung\Downloads\drive-download-20200111T023258Z-001\f2pyTest/testsub.f90:3: undefined reference to `__testmod_MOD_sub_in_mod'
collect2.exe: error: ld returned 1 exit status
error: Command "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe -Wall -g -Wall -g -shared ..\..\..\..\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\testsub.o -LC:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0 -Lc:\users\thein lin aung\appdata\local\programs\python\python38\libs -Lc:\users\thein lin aung\appdata\local\programs\python\python38\PCbuild\amd64 -o C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\.libs\libtestsub.APU2XWIZSCE75AFY5M5MJDAHS5PXOMHM.gfortran-win_amd64.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\THEINL~1\AppData\Local\Temp\tmp1mpqikyt\Release\libtestsub.APU2XWIZSCE75AFY5M5MJDAHS5PXOMHM.gfortran-win_amd64.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64" failed with exit status 1

Tags: includeenablelocalwithstaticusersappdatatemp

热门问题