【问题标题】:Are there any working examples of how to use the scikit-build with f2py?有没有关于如何将 scikit-build 与 f2py 一起使用的工作示例?
【发布时间】:2021-01-15 23:48:12
【问题描述】:

scikit-build 发行版提供了 FindF2PY 和 UseF2PY 的使用示例,但它们并不完整,仅提供了部分 CMakeLists.txt 文件而没有其他必需的文件。根据文档,我无法做出可以构建的东西。

按照 scikit-build 文档中的示例,我创建了以下文件:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.10.2)

project(skbuild_test)

enable_language(Fortran)

find_package(F2PY REQUIRED)

add_f2py_target(f2py_test f2py_test.f90)
add_library(f2py_test MODULE f2py_test.f90)
install(TARGETS f2py_test LIBRARY DESTINATION f2py_test)

setup.py:

import setuptools
from skbuild import setup

requires=['numpy']

setup(
    name="skbuild-test",
    version='0.0.1',
    description='Performs line integrals through SAMI3 grids',
    author='John Haiducek',
    requires=requires,
    packages=['f2py_test']
)

f2py_test.f90:

module mod_f2py_test
  implicit none
contains
  subroutine f2py_test(a,b,c)
    real(kind=8), intent(in)::a,b
    real(kind=8), intent(out)::c
  end subroutine f2py_test
end module mod_f2py_test

此外,我创建了一个目录 f2py_test,其中包含一个空的 init.py。

python setup.py develop 的输出显示 scikit-build 调用 CMake 并编译我的 Fortran 代码。但是,在编译 f2py 包装代码时找不到 Python.h:

[2/7] Building C object CMakeFiles/_f2...kages/numpy/f2py/src/fortranobject.c.o
FAILED: CMakeFiles/_f2py_runtime_library.dir/venv/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.c.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc   -O3 -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -mmacosx-version-min=10.14 -MD -MT CMakeFiles/_f2py_runtime_library.dir/venv/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.c.o -MF CMakeFiles/_f2py_runtime_library.dir/venv/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.c.o.d -o CMakeFiles/_f2py_runtime_library.dir/venv/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.c.o   -c ../../../venv/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.c
In file included from ../../../venv/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.c:2:
../../../venv/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.h:7:10: fatal error: 'Python.h' file not found
#include "Python.h"
         ^~~~~~~~~~
1 error generated.

【问题讨论】:

    标签: python cmake f2py


    【解决方案1】:

    首先需要注意的是,可能有更好的方法来做到这一点,因为我刚刚通过偶然发现您的帖子并查看文档来了解如何使 scikit-build 工作。第二个警告,我也在学习 cmake。所以,也许有更好的办法。

    您需要做几件事才能使您的示例在这里工作。最重要的是 add_f2py_target() 的第二个参数不是源文件。它要么是预生成的 *.pyf 的名称,要么是让 f2py 生成一个,为它提供一个不带 *.pyf 扩展名的参数。另一个是为各种组件添加包含目录。

    我使您的示例与以下 CMakeLists.txt 一起工作:

    cmake_minimum_required(VERSION 3.10.2)
    project(skbuild_test)
    enable_language(Fortran)
    find_package(F2PY REQUIRED)
    find_package(PythonLibs REQUIRED)
    find_package(Python3 REQUIRED COMPONENTS NumPy)
    
    #the following command either generates or points to an existing .pyf
    #if provided an argument with .pyf extension, otherwise f2py generates one (not source code).
    add_f2py_target(f2py_test f2py_test)
    add_library(f2py_test MODULE f2py_test.f90)
    include_directories(${PYTHON_INCLUDE_DIRS})
    include_directories(${_Python3_NumPy_INCLUDE_DIR})
    target_link_libraries(f2py_test ${PYTHON_LIBRARIES})
    install(TARGETS f2py_test LIBRARY DESTINATION f2py_test)
    

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 2020-03-13
      • 2010-11-15
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2019-06-03
      相关资源
      最近更新 更多