F2PY: неопределенная ссылка на подпрограмму в модуле - PullRequest
0 голосов
/ 06 января 2020

Я пытаюсь использовать f2py для генерации интерфейса 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

Цель состоит в том, чтобы позвонить subroutine_py() из Python. В соответствии с инструкциями, которые я нашел в Интернете, я выполнил следующие команды:

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. Я обнаружил схожие проблемы со стековым потоком, но ни одна из них напрямую не решала проблему.

Edit

Согласно комментарию от 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
...