简体   繁体   English

为C ++编译时无法使Cython工作

[英]Can't get Cython to work when compiling for C++

I'm trying do a simple example to cythonize a c++ Test class. 我正在尝试做一个简单的例子来cythonize c ++测试类。 I can't get it to work, why? 我无法正常工作,为什么呢?

Here is my code, very basic: 这是我的代码,非常基本:

mytest.h: mytest.h:

class Test
{
public:
    Test(unsigned test = 0);

    void print();
private:
    unsigned m_test;

}; };

mytest.cpp: mytest.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;
}

For the Cython part, I have, test.pyx : 对于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

And i compile with: 我编译:

cython --cplus test.pyx

... And get a ton of error messages, like "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

What am I not seeing ? 我没看到什么?

Thanks 谢谢

I've been where you are few weeks ago. 几周前我去过你那里。 Since I am a new user of Cython as well, I can't say for sure, but giving following advice. 由于我也是Cython的新用户,因此我不能肯定地说,但请提供以下建议。

You may want to put following lines at the top of your .pyx file (very easy to ignore in Cython tutorial) 您可能需要将以下行放在.pyx文件的顶部(在Cython教程中非常容易忽略)

# distutils: language = c++
# distutils: sources = mytest.cpp.

And for compile command, you can use: 对于编译命令,您可以使用:

cython -a test.pyx --cplus

Hope it helps. 希望能帮助到你。 :) :)

I had exactly the same problem a few days ago. 几天前我遇到了完全相同的问题。 The issue is with the name of your print() method. 问题出在您的print()方法的名称上。 For some reason, which I dont know exactly, cython doesn't like print(). 由于某些原因,我不十分了解,cython不喜欢print()。 If you change it to any other name, like printtest(). 如果将其更改为其他任何名称,例如printtest()。 It will compile just fine. 它将编译就好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM