在 ipython 中用 Cython 包装 C++ 标准库
Wrapping C++ Standard library with Cython in ipython
根据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 答案的替代方案,documentation for ipython Cython magic 建议使用
%%cython --cplus
打开 C++ 模式。该命令的帮助也可以通过 IPython 控制台中的 运行 %%cython?
访问。
就我个人而言,我认为使用 distutils 注释方法有很多值得一提的地方,因为它将语言与需要它的代码联系起来。
根据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 答案的替代方案,documentation for ipython Cython magic 建议使用
%%cython --cplus
打开 C++ 模式。该命令的帮助也可以通过 IPython 控制台中的 运行 %%cython?
访问。
就我个人而言,我认为使用 distutils 注释方法有很多值得一提的地方,因为它将语言与需要它的代码联系起来。