包装C++;ipython中带有Cython的标准库

包装C++;ipython中带有Cython的标准库,python,c++,cython,Python,C++,Cython,根据Cython文档,我编写了如下Cython代码: In [1]:%load_ext Cython In [2]: %%cython from libcpp.vector cimport vector ​ cdef vector[int] *vec_int = new vector[int](10) 编译后,ipython生成以下错误: Error compiling Cython file: --------------------------------

根据Cython文档,我编写了如下Cython代码:

In [1]:%load_ext Cython
In [2]: %%cython
         from libcpp.vector cimport vector
​         cdef vector[int] *vec_int = new vector[int](10)
编译后,ipython生成以下错误:

Error compiling Cython file:
------------------------------------------------------------ 
... 
from libcpp.vector cimport vector 
cdef vector[int] *vec_int = new vector[int](10) 
                               ^ 
------------------------------------------------------------
/Users/m/.ipython/cython/_cython_magic_a72abb419ccf1b31db9a1851b522a4bf.pyx:3:32: Operation only allowed in c++

我的代码出了什么问题?

你需要通过特别注释告诉cython你正在编译
C++
,而不是
C

# distutils: language = c++

%%cython
块后添加此选项将解决您的问题。

作为@romeric答案的替代方案,建议使用

%%cython --cplus

打开C++模式。也可以通过在IPython控制台中运行

%%cython?
来访问该命令的帮助

我个人认为使用distutils注释方法有很多值得一提的地方,因为它将语言与需要它的代码联系起来