Cython c ++示例无法识别c ++,为什么? [英] Cython c++ example fails to recognize c++, why?

查看:147
本文介绍了Cython c ++示例无法识别c ++,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Cython C ++网页上建立'using c ++ in cython'的例子, a>,但是设置似乎不能识别语言c ++。



取自同一页面的文件是:



Rectangle.cpp

  #includeRectangle.h

使用命名空间形状;

Rectangle :: Rectangle(int X0,int Y0,int X1,int Y1){
x0 = X0;
y0 = Y0;
x1 = X1;
y1 = Y1;
}

Rectangle ::〜Rectangle(){}

int Rectangle :: getLength(){
return(x1 - x0);
}

int Rectangle :: getHeight(){
return(y1 - y0);
}

int Rectangle :: getArea(){
return(x1 - x0)*(y1 - y0);
}

void Rectangle :: move(int dx,int dy){
x0 + = dx;
y0 + = dy;
x1 + = dx;
y1 + = dy;
}






矩形。 h:

 命名空间形状{
class Rectangle {
public:
int x0,y0,x1,y1;
Rectangle(int x0,int y0,int x1,int y1);
〜Rectangle();
int getLength();
int getHeight();
int getArea();
void move(int dx,int dy);
};
}






pyx

  cdef extern fromRectangle.hnamespaceshapes:
cdef cppclass Rectangle:
Rectangle(int,int,int,int)
int x0,y0,x1,y1
int getLeight()
int getHeight
void move(int,int)

cdef类PyRectangle:
cdef Rectangle * thisptr#保存一个我们包装的C ++实例
def __cinit __(self, int x0,int y0,int x1,int y1):
self.thisptr = new Rectangle(x0,y0,x1,y1)
def __dealloc __(self):
del self.thisptr
def getLength(self):
return self.thisptr.getLength()
def getHeight(self):
return self.thisptr.getHeight()
def getArea (self):
return self.thisptr.getArea()
def move(self,dx,dy):
self.thisptr.move(dx,dy)






setup.py
$ b

 来自distutils.core import setup 
来自Cython.Build import cythonize

setup(ext_modules = cythonize $ brectangle.pyx,#我们的Cython源
sources = [Rectangle.cpp],#附加源文件
language =c ++,#generate C ++代码
))






  python setup.py --build_ext --inplace 

但是这会失败,并显示Operation only allowed in c ++的错误提示语言=c ++选项未正确传递。

  cython rectangle.pyx --cplus 

会产生一个c ++文件,但我希望能够使setup.py方法起作用。



错误文本是:

  Cython化rectangle.pyx 

编译Cython文件时出错:
- -------------------------------------------------- ---------
...
void move(int,int)

cdef类PyRectangle:
cdef Rectangle * thisptr#hold a我们包装的C ++实例
def __cinit __(self,int x0,int y0,int x1,int y1):
self.thisptr = new Rectangle(x0,y0,x1,y1)
^
------------------------------------------- -----------------

rectangle.pyx:13:27:只允许在c ++中操作

编译Cython文件时出错:
--------------------------------------------- ---------------
...
cdef类PyRectangle:
cdef Rectangle * thisptr#包含一个我们包装的C ++实例
def __cinit __(self,int x0,int y0,int x1,int y1):
self.thisptr = new Rectangle(x0,y0,x1,y1)
def __dealloc __ b $ b del self.thisptr
^
---------------------------------- --------------------------

rectangle.pyx:15:8:只允许在c ++ $ b $中操作b Traceback(最近一次调用):
文件setup.py,第8行,在< module>
language =c ++,#generate C ++ code
File/Library/Python/2.7/site-packages/Cython-0.19.1-py2.7-macosx-10.8intel.egg/Cython/ Build / Dependencies.py,行753,在cythonize
cythonize_one(* args [1:])
文件/Library/Python/2.7/site-packages/Cython-0.19.1-py2。 7-macosx-10.8intel.egg / Cython / Build / Dependencies.py,第820行,在cythonize_one
raise CompileError(None,pyx_file)
Cython.Compiler.Errors.CompileError:rectangle.pyx


解决方案



但是,除了一些其他差异(IndentationError,命名文件 rectangle.pyx 而不是 rect.pyx ,...),结果证明这与这个问题没有关系,你还在Cython文件的顶部留下了这个:

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

如果我离开,我得到相同的错误。


I'm attempting to build to the example for 'using c++ in cython' at the Cython C++ page, but setup appears not to recognize the language, c++.

The files, taken from that same page are:

Rectangle.cpp

#include "Rectangle.h"

using namespace shapes;

Rectangle::Rectangle(int X0, int Y0, int X1, int Y1){
    x0 = X0;
    y0 = Y0;
    x1 = X1;
    y1 = Y1;
}

Rectangle::~Rectangle() {}

int Rectangle::getLength() {
    return (x1 - x0);
}

int Rectangle::getHeight() {
    return (y1 - y0);
} 

int Rectangle::getArea() {
    return (x1 - x0) * (y1 - y0);
}

void Rectangle::move(int dx, int dy) {
    x0 += dx;
    y0 += dy;
    x1 += dx;
    y1 += dy;
}


Rectangle.h:

namespace shapes {
    class Rectangle {
    public:
    int x0, y0, x1, y1;
    Rectangle(int x0, int y0, int x1, int y1);
    ~Rectangle();
    int getLength();
    int getHeight();
    int getArea();
    void move(int dx, int dy);
    };
 } 


rectangle.pyx

cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
    Rectangle(int, int, int, int)
    int x0, y0, x1, y1
    int getLength()
    int getHeight()
    int getArea()
    void move(int, int)

cdef class PyRectangle:
    cdef Rectangle *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.thisptr = new Rectangle(x0, y0, x1, y1)
    def __dealloc__(self):
        del self.thisptr
    def getLength(self):
        return self.thisptr.getLength()
    def getHeight(self):
        return self.thisptr.getHeight()
    def getArea(self):
        return self.thisptr.getArea()
    def move(self, dx, dy):
        self.thisptr.move(dx, dy)


setup.py

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules = cythonize(
       "rectangle.pyx",            # our Cython source
       sources=["Rectangle.cpp"],  # additional source file(s)
       language="c++",             # generate C++ code
      ))


I attempt to build this with:

python setup.py --build_ext --inplace

But this fails, with the error "Operation only allowed in c++" suggesting that the language="c++" option is not being passed correctly.

cython rectangle.pyx --cplus

does produce a c++ file, but I was hoping to be able to get the setup.py method to work.

The entire error text is:

Cythonizing rectangle.pyx

Error compiling Cython file:
------------------------------------------------------------
...
        void move(int, int)

cdef class PyRectangle:
    cdef Rectangle *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.thisptr = new Rectangle(x0, y0, x1, y1)
                      ^
------------------------------------------------------------

rectangle.pyx:13:27: Operation only allowed in c++

Error compiling Cython file:
------------------------------------------------------------
...
cdef class PyRectangle:
    cdef Rectangle *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.thisptr = new Rectangle(x0, y0, x1, y1)
    def __dealloc__(self):
        del self.thisptr
       ^
------------------------------------------------------------

rectangle.pyx:15:8: Operation only allowed in c++
Traceback (most recent call last):
  File "setup.py", line 8, in <module>
    language="c++",             # generate C++ code
  File "/Library/Python/2.7/site-packages/Cython-0.19.1-py2.7-macosx-10.8intel.egg/Cython/Build/Dependencies.py", line 753, in cythonize
    cythonize_one(*args[1:])
  File "/Library/Python/2.7/site-packages/Cython-0.19.1-py2.7-macosx-10.8intel.egg/Cython/Build/Dependencies.py", line 820, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: rectangle.pyx

解决方案

Following the example exactly, on a very similar system to yours, everything works fine for me.

But, along with a few other differences (an IndentationError, naming the file rectangle.pyx instead of rect.pyx, …) which turned out not to be relevant to this problem, you also left off this at the top of your Cython file:

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

And if I leave that off, I get the same error.

这篇关于Cython c ++示例无法识别c ++,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆