[英]Error when wrapping helloworld in c++ into python with cython
I have the following files: 我有以下文件:
helloworld.cpp
which contains helloworld.cpp
其中包含
#include <iostream>
#include <Python.h>
void Helloworld(){
std::cout << "Hello world!" << "\n";
}
helloworld.pyx
which contains: helloworld.pyx
包含:
cdef extern from "helloworld.cpp":
cpdef void Helloworld()
and setup.py
which contains: 和
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]))
When I run the following command in Ipython it builds correctly 当我在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
but when I attempt to import it I get the following error: 但是当我尝试导入它时,出现以下错误:
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)
I have also tried the following: 我也尝试了以下方法:
helloworld.pyx
containing: helloworld.pyx
包含:
cdef extern from "helloworld.cpp":
void Helloworld()
def C_Helloworld():
return Helloworld()
In which case when I try to build it I get: 在这种情况下,当我尝试构建它时,我得到:
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,
...
which implied it is being called recursively. 这意味着它被递归调用。
I think it's because your module is empty and does not define any function. 我认为这是因为您的模块为空并且没有定义任何功能。
Cython does not create the wrapper. Cython不会创建包装。 You have to tell him how to use your c++ functions.
您必须告诉他如何使用c ++函数。
Also the content of your ".pyx" file is typically in a ".pxd" file. 同样,“。pyx”文件的内容通常位于“ .pxd”文件中。 The ".pyx" file contains the functions that will be called from python.
“ .pyx”文件包含将从python调用的函数。 Example :
范例:
def hello_world():
Helloworld()
The problem was two-fold. 问题是双重的。 One was that, as manawy stated, I didn't have a wrapper python function to call the C++ function.
正如manawy所说,其中之一是我没有包装器python函数来调用C ++函数。 The other issue was that my C++ file had the same name as the .pyx file so when it compiled it, it overwrote the C++ file recursively.
另一个问题是我的C ++文件与.pyx文件具有相同的名称,因此在编译时,它会递归地覆盖C ++文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.