使用 cython 将 c++ 中的 helloworld 包装到 python 中时出错
Error when wrapping helloworld in c++ into python with cython
我有以下文件:
包含 helloworld.cpp
#include <iostream>
#include <Python.h>
void Helloworld(){
std::cout << "Hello world!" << "n";
}
helloworld.pyx
包含:
cdef extern from "helloworld.cpp":
cpdef void Helloworld()
以及包含以下内容setup.py
:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext = Extension('helloworld', sources=["helloworld.pyx"], language="c++")
setup(name="helloworld", ext_modules = cythonize([ext]))
当我在 Ipython 中运行以下命令时,它会正确构建
In [1]: run setup.py build_ext --inplace --verbose
running build_ext
building 'helloworld' extension
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/ash/anaconda2/envs/python3/include/python3.5m -c helloworld.cpp -o build/temp.linux-x86_64-3.5/helloworld.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
g++ -pthread -shared -L/home/ash/anaconda2/envs/python3/lib -Wl,-rpath=/home/ash/anaconda2/envs/python3/lib,--no-as-needed build/temp.linux-x86_64-3.5/helloworld.o -L/home/ash/anaconda2/envs/python3/lib -lpython3.5m -o /home/ash/CallingC++fromPython/Cython/HelloWorld/helloworld.cpython-35m-x86_64-linux-gnu.so
但是当我尝试导入它时,出现以下错误:
In [2]: import helloworld
------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-12-9f213747d34d> in <module>()
----> 1 import helloworld
ImportError: dynamic module does not define module export function (PyInit_helloworld)
我还尝试了以下方法:
helloworld.pyx
包含:
cdef extern from "helloworld.cpp":
void Helloworld()
def C_Helloworld():
return Helloworld()
在这种情况下,当我尝试构建它时,我会得到:
run setup.py build_ext --inplace --verbose
running build_ext
building 'helloworld' extension
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/ash/anaconda2/envs/python3/include/python3.5m -c helloworld.cpp -o build/temp.linux-x86_64-3.5/helloworld.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
In file included from helloworld.cpp:458:0,
from helloworld.cpp:458,
from helloworld.cpp:458,
from helloworld.cpp:458,
...
from helloworld.cpp:458,
from helloworld.cpp:458,
from helloworld.cpp:458:
helloworld.cpp:16:20: error: #include nested too deeply
helloworld.cpp:23:20: error: #include nested too deeply
helloworld.cpp:163:27: error: #include nested too deeply
In file included from helloworld.cpp:458:0,
from helloworld.cpp:458,
from helloworld.cpp:458,
...
这意味着它被递归调用。
我认为
这是因为您的模块是空的并且没有定义任何函数。
Cython 不会创建包装器。你必须告诉他如何使用你的c ++函数。
此外,".pyx"文件的内容通常位于".pxd"文件中。".pyx"文件包含将从python调用的函数。例:
def hello_world():
Helloworld()
问题是双重的。一个是,正如manawy所说,我没有一个包装器python函数来调用C++函数。另一个问题是我的C++文件与 .pyx 文件同名,因此当它编译它时,它会递归覆盖C++文件。
相关文章:
- 将代码从 Python 转换为 C++ 时出错
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- 通过std::system调用python时出错
- c++ 上的嵌入式 python.更改调试器时出错
- 从 Python 调用 Windows DLL 函数时出错
- Python 3.6 的 boost 和 dlib 安装期间出错
- 编译在 Python 代码内部调用的 C++ 代码时出错,使用 Boost Python
- 导入 Boost Python 模块 (function_impl_base9max_arityEv) 时出错
- Boost Python:在函数中通过引用传递变量时出错
- 使用 cython 将 c++ 中的 helloworld 包装到 python 中时出错
- pip 在 Python 3 Windows 10 x86 中安装 CRFsuite 时出错
- 从Python扩展调用C++虚拟成员时出错
- 返回对自定义对象的引用时 Boost.Python 出错
- 使用 mingw 和 msis 编译带有 Python 绑定的 c++ 时出错
- Python 安装包在 msvccompiler 上出错
- Pretty Printers在Eclipse Ubuntu 14中执行Python代码时出错
- 安装用C++编写的Python包(leven)时出错:g++无法识别的命令行选项--输出lib
- 在Python中使用回调时出错
- 在Boost Python中运行bjam时出错
- 生成boost:python代码时出错