c++ - 为 C++ 编译时无法让 Cython 工作

标签 c++ compilation cython

我正在尝试做一个简单的例子来 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/

相关文章:

c++ - 重载 unique_ptr 运算符调用 make_unique

c++ - C++中如何链接头文件

c# - C# 编译器会去除空方法吗?

python - Cython 的意外输出和返回值

cython - 在 Python 中引用 Cython 常量

c++ - 将静态 const 成员重新声明为 constexpr 是否会自动将其限定为内联?

c++ - 什么是游标链表? [C++]

c++ - 路径变得太长的解决方法?

c++ - 默认构造函数编译错误,预期的ID不合格

python - Cython - 将 C++ 函数返回的 C++( vector 和非 vector )对象公开给 Python