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

2024-09-29 22:05:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

假设c++代码在header.hpp中,如下所示

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

我需要在.pyx文件和设置.py文件,以便我可以在Python中使用函数作为

^{pr2}$

PS:在PYPY中可以访问相同的函数吗?如果是,怎么办?在


谢谢你的帮助。但在我试图追随你的道路时,我还有更多的困难(见下文)。在

双字节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)

在双字节.pyx在

# main.pyx file
from cdoublit cimport *

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

以及设置.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

Tags: 函数frompybuildsetupfunctionerrorext
1条回答
网友
1楼 · 发布于 2024-09-29 22:05:57

Cython.0的语法只支持Cython.0的类。在

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

# 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。在

相关问题 更多 >

    热门问题