gpt4 book ai didi

python - 在 Cython 中调用外部 c++ 模板函数

转载 作者:太空狗 更新时间:2023-10-29 21:53:14 26 4
gpt4 key购买 nike

我在 C++ 头文件中声明和实现了许多 C++ 模板函数,我想访问 Cython 中的一些函数。

假设header.hpp中的c++代码如下

template <class T> 
T doublit(T& x) {
return 2*x;
}

我需要在 .pyx 文件和 setup.py 文件中写入什么,以便我可以在 Python 中使用该函数作为

>>> import modname
>>> print modname.doublit(3)
6

PS:是否可以在 PYPY 中访问相同的功能?而且,如果是,如何?


感谢您的帮助。但是,当我尝试按照您的方式进行操作时,我遇到了更多困难(如下)。

doublit.h

template <class T> 
T doublit(T& x) {
return 2*x;
}

cdoublit.pxd

cdef extern from "doublit.h":
cdef int doublit1 "doublit<int>"(int& foo)
cdef double doublit2 "doublit<double>"(double& foo)

doublit.pyx

# main.pyx file
from cdoublit cimport *

cdef int n1 = 5
cdef double n2 = 5.0
print(doublit1(n1))
print(doublit2(n2))

和setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("doublit", ["doublit.pyx"])]

setup(
name = 'Learning Cython',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)

最后,我构建为

>>> python setup.py build_ext --inplace

但我得到以下异常:

###:doublit markma$ python setup.py build_ext --inplace
running build_ext
cythoning doublit.pyx to doublit.c
building 'doublit' extension
creating build
creating build/temp.macosx-10.6-intel-2.7
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -g -O2 -DNDEBUG -g -O3 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c doublit.c -o build/temp.macosx-10.6-intel-2.7/doublit.o
In file included from doublit.c:311:
doublit.h:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
doublit.c: In function ‘initdoublit’:
doublit.c:782: error: ‘doublit’ undeclared (first use in this function)
doublit.c:782: error: (Each undeclared identifier is reported only once
doublit.c:782: error: for each function it appears in.)
doublit.c:782: error: expected expression before ‘int’
doublit.c:793: error: expected expression before ‘double’
In file included from doublit.c:311:
doublit.h:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
doublit.c: In function ‘initdoublit’:
doublit.c:782: error: ‘doublit’ undeclared (first use in this function)
doublit.c:782: error: (Each undeclared identifier is reported only once
doublit.c:782: error: for each function it appears in.)
doublit.c:782: error: expected expression before ‘int’
doublit.c:793: error: expected expression before ‘double’
lipo: can't figure out the architecture type of: /var/folders/ip/ip5rkteZFbWPEtzhmxRdVE+++Tc/-Tmp-//ccvaEGqZ.out
error: command 'gcc-4.2' failed with exit status 1

最佳答案

Cython 支持模板语法,但仅支持类(从 Cython 0.19.1 开始)。

尽管您可以使用以下语法包装模板函数:

# doublit.pxd file
cdef extern from "doublit.h":
cdef int doublit1 "doublit<int>"(int& foo)
cdef double doublit2 "doublit<double>"(double& foo)

# main.pyx file
from doublit cimport *
cdef int n1 = 5
cdef double n2 = 5.0
print(doublit1(n1))
print(doublit2(n2))

你失去了自动化,但至少你可以让它发挥作用。

更新

Cython 0.20 添加了对调用 C++ 模板函数的支持。 Cython 0.20 beta release announced .

关于python - 在 Cython 中调用外部 c++ 模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17999703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com