Python numpy/f2py 连接库

2 投票
1 回答
1900 浏览
提问于 2025-04-17 20:34

我正在尝试为我的一个Fortran程序制作一个包装器,使用的是f2py。我的Fortran程序使用了一些外部库,当我尝试链接这些库时,出现了一个错误,提示如下:

gfortran:f77: /var/folders/46/l1mrxgls07s6tpwb6tgpvhpr0000gn/T/tmpPCM7Ne/src.macosx-10.9-intel-2.7/progs-f2pywrappers.f
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/bin/f2py", line 24, in <module>
    main()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/f2py/f2py2e.py", line 588, in main
    run_compile()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/f2py/f2py2e.py", line 574, in run_compile
    setup(ext_modules = [ext])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/core.py", line 186, in setup
    return old_setup(**new_attr)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/command/build.py", line 37, in run
    old_build.run(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build.py", line 127, in run
    self.run_command(cmd_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/command/build_ext.py", line 232, in run
    self.build_extensions()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build_ext.py", line 448, in build_extensions
    self.build_extension(ext)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/command/build_ext.py", line 425, in build_extension
    build_temp=self.build_temp,**kws)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 691, in link_shared_object
    extra_preargs, extra_postargs, build_temp, target_lang)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/fcompiler/__init__.py", line 643, in link
    libraries)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/ccompiler.py", line 571, in gen_lib_options
    runtime_library_dirs, libraries)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 1086, in gen_lib_options
    lib_file = compiler.find_library_file([lib_dir], lib_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 777, in find_library_file
    raise NotImplementedError

看起来这个错误是来自Python库中的一个文件ccompiler.py

这个错误对应于文件中的以下几行代码:

def find_library_file (self, dirs, lib, debug=0):
        """Search the specified list of directories for a static or shared
        library file 'lib' and return the full path to that file.  If
        'debug' true, look for a debugging version (if that makes sense on
        the current platform).  Return None if 'lib' wasn't found in any of
        the specified directories.
        """
        raise NotImplementedError

我对如何解决这个问题有点困惑,因为我对Python还不太熟悉。似乎这个方法还没有被实现。我是不是需要自己来实现它?具体该怎么做呢?我需要在做这个修改后重新构建f2py的可执行文件吗?如果需要,我该怎么做呢?

谢谢!

1 个回答

1

这个问题在这里没有得到解答,但它提供了一种解决方法(直接引用所有的*.o文件,而不是把它们放在一个库里)。而且,这篇文章很好地解释了一些关于f2py的内容。

在用f2py包装的模块中包含一个已编译的模块(最小工作示例)?

f2py -c --fcompiler=gfortran -I. libtest.o -m Main main.f90

如果你的库是基于一个单独的文件,这个命令是可以工作的。如果你的库是(更常见的情况)基于多个文件,那就需要把所有的*.o文件名都包含进去。

撰写回答