我正在围绕C和Fortran库创建Python包装器。我希望这个C/Fortran库中的代码由pip编译,这样用户在运行pip之前就不需要自己安装库了。我目前使用Numpy Distutils实现这一点,setup.py与此类似:
#!/usr/bin/env python
def configuration(parent_package='', top_path=None):
from numpy.distutils.misc_util import Configuration
config = Configuration('name', parent_package, top_path)
f90_src = [<list of .f90 files>]
c_src = [<list of .c files>]
src = c_src + f90_src
config.add_library('libname', sources=src,
extra_f90_compile_args=['-pthread', '-fno-underscoring'])
config.add_extension(name='wrapper_name', sources=['wrapper_name.f90'],
extra_f90_compile_args=['-pthread'], libraries=['libname'],
module_dirs=['build/temp.linux-x86_64-3.7/'])
return config
if __name__ == "__main__":
from numpy.distutils.core import setup
setup(configuration=configuration)
库包含Fortran模块,因此在编译调用库的包装器(以及use
的模块)时,我需要告诉编译器在哪里可以找到模块。这就是module_dirs
选项。问题是我不得不硬编码。是否有一种可移植且健壮的方法来查找此目录的路径
请注意,我必须使用add_library
来编译我正在包装的代码,而不是将它放在add_extension
的sources
中,就像我做了后者一样f2py
将尝试在该代码中包装所有接口,但它使用了f2py
不支持的Fortran的某些特性,因此编译失败
一种解决方法可能是将代码更改为使用接口而不是模块,但我正在包装的代码非常大,对其进行任何如此大的修改都是不可行的
config.get_build_temp_dir()
函数听起来很有希望,但如果在需要它的地方调用它(表示它尚未初始化),则会引发错误
目前没有回答
相关问题 更多 >
编程相关推荐