f2py无法使用回调函数包装子例程

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

我正在尝试从f2py docs编译示例代码,该代码从子例程调用一个外部回调函数,该例程本身是从另一个子例程调用的:

      subroutine f1()
         print *, "in f1, calling f2 twice.."
         call f2()
         call f2()
         return
      end

      subroutine f2()
cf2py    intent(callback, hide) fpy
         external fpy
         print *, "in f2, calling f2py.."
         call fpy()
         return
      end

但是,当我尝试将其包装时

f2py -c -m test test.f

我收到此错误:

C:\f2py-test/test.f90:12: undefined reference to `fpy_'
C:\f2py-test/test.f90:12: undefined reference to `fpy_'
C:\f2py-test/test.f90:12: undefined reference to `fpy_'
collect2.exe: error: ld returned 1 exit status
error: Command "C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\gfortran.exe -Wall -g -Wall -g -shared ..\Users\ABM\AppData\Local\Temp\tmpden52paw\Release\test.o -LC:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0 -LC:\Users\ABM\AppData\Local\Programs\Python\Python36\libs -LC:\Users\ABM\AppData\Local\Programs\Python\Python36\PCbuild\amd64 -o C:\Users\ABM\AppData\Local\Temp\tmpden52paw\Release\.libs\libtest.4DNBIXDTB2Q2CUYHKR2KFI6QGJEG7OZ7.gfortran-win_amd64.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\ABM\AppData\Local\Temp\tmpden52paw\Release\libtest.4DNBIXDTB2Q2CUYHKR2KFI6QGJEG7OZ7.gfortran-win_amd64.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64" failed with exit status 1

但是,如果我将回调函数作为参数传递并在每个子例程中调用它,则可以编译此代码:

      subroutine f1(fpy)
         external fpy
         call fpy()
         print *, "in f1, calling f2 twice.."
         call f2(fpy)
         call f2(fpy)
         return
      end

      subroutine f2(fpy)
         external fpy
         print *, "in f2, calling f2py.."
         call fpy()
         return
      end

但是这种方法在实际情况下变成了一个艰巨的任务,需要大量嵌套子例程。

那么,如何解决此问题?

python numpy fortran f2py
1个回答
0
投票

我正面临着完全相同的问题,您发现任何问题吗?

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.