问题描述
我想大部分时间在cython中使用c ++功能,我发现使用pyximport非常方便,但是为每个pyx模块制作pyxbld配置文件(如)很烦人。我可以配置pyximport始终为所有pyx模块生成c ++输出吗?
I want to use c++ functionality most of the time in cython and I find using pyximport very convenient, but making pyxbld configuration file for each pyx module (as described in How do you tell pyximport to use the cython --cplus option?) is tiresome. Can I configure pyximport to always make a c++ output for all pyx modules?
推荐答案
这是一个hack。
以下代码对 pyximport
中的 get_distutils_extension
函数进行了猴子修补,以便扩展对象的语言
属性均设置为 c ++
The following code monkey-patches the get_distutils_extension
function in pyximport
so that the Extension
objects it creates all have their language
attribute set to c++
.
import pyximport
from pyximport import install
old_get_distutils_extension = pyximport.pyximport.get_distutils_extension
def new_get_distutils_extension(modname, pyxfilename, language_level=None):
extension_mod, setup_args = old_get_distutils_extension(modname, pyxfilename, language_level)
extension_mod.language='c++'
return extension_mod,setup_args
pyximport.pyximport.get_distutils_extension = new_get_distutils_extension
输入上面的代码在pyximportcpp.py中。然后,不要使用 import pyximport; pyximport.install()
,使用 import pyximportcpp; pyximportcpp.install()
。
Put the above code in pyximportcpp.py. Then, instead of using import pyximport; pyximport.install()
, use import pyximportcpp; pyximportcpp.install()
.
这篇关于如何配置pyximport始终创建一个cpp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!