不能用 c++ 编译 cython

Cant compile cython with c++

本文关键字:cython 编译 c++ 不能      更新时间:2023-10-16

我试图在 上运行cython示例http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html我基本上只是复制了Rectangle.h,Rectangle.cpp,setup.py 和rect.pyx中的代码但是,当我跑步时Python setup.py build_ext --inplace我收到错误

running build_ext
building 'rect' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c rect.c -o build/temp.linux-x86_64-2.7/rect.o
In file included from rect.c:235:0:
Rectangle.h:1:1: error: unknown type name ‘namespace’
Rectangle.h:1:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
rect.c:236:15: fatal error: ios: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

我做错了什么???

Rectangle.h:1:1:1:错误:未知类型名称"命名空间"

namespace仅由C++编译器识别。我猜你的意思是使用 g++ 而不是 gcc 编译器。将build_ext更改为使用 g++,并且为了清楚起见,请将文件重命名为矩形.cpp

在 setup.py 脚本中,将语言设置为 c++ ext_modules

...
ext_modules=[
    Extension("rect",
    sources=["rect.pyx"],
    language="c++",
    )]
setup(
  name = 'rect',
  ext_modules = cythonize(ext_modules),
)

Cython 现在将调用正确的 c++ 编译器