我正在尝试做一个简单的例子来 cythonize 一个 C++ 测试类。我无法让它工作,为什么?
这是我的代码,非常基础:
mytest.h:
class Test
{
public:
Test(unsigned test = 0);
void print();
private:
unsigned m_test;
};
我的测试.cpp:
#include "mytest.h"
#include <iostream>
using namespace std;
Test::Test(unsigned test)
: m_test(test)
{
cout << "Test::Test" << endl;
}
void Test::print()
{
cout << "print:" << m_test << endl;
}
对于 Cython 部分,我有 test.pyx:
cdef extern from "mytest.h":
cdef cppclass Test:
Test(unsigned int) except +
void print()
cdef class pyTest:
cdef Test* thisptr
def __cinit__(self, unsigned test):
self.thisptr = new Test(test)
def __dealloc__(self):
del self.thisptr
然后我编译:
cython --cplus test.pyx
... 并收到大量错误消息,例如“Empty declarator”:
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> cdef extern from "mytest.h":
> cdef cppclass Test:
> Test(unsigned int) except +
> void print()
> ^
> ------------------------------------------------------------
>
> test.pyx:4:7: Empty declarator
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> cdef extern from "mytest.h":
> cdef cppclass Test:
> Test(unsigned int) except +
> void print()
> ^
> ------------------------------------------------------------
>
> test.pyx:4:7: Syntax error in C variable declaration
我没看到什么?
谢谢
最佳答案
几周前我去过你所在的地方。由于我也是 Cython 的新用户,我不能肯定地说,但给出以下建议。
您可能希望将以下行放在 .pyx 文件的顶部(在 Cython 教程中很容易忽略)
# distutils: language = c++
# distutils: sources = mytest.cpp.
对于编译命令,您可以使用:
cython -a test.pyx --cplus
希望对您有所帮助。 :)
关于c++ - 为 C++ 编译时无法让 Cython 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19402494/