【问题标题】:Error when wrapping helloworld in c++ into python with cython使用 cython 将 c++ 中的 helloworld 包装到 python 中时出错
【发布时间】:2017-01-17 16:59:22
【问题描述】:

我有以下文件:

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,
...

这意味着它被递归调用。

【问题讨论】:

    标签: python c++ cython cythonize


    【解决方案1】:

    我认为是因为你的模块是空的,没有定义任何函数。

    Cython 不会创建包装器。你必须告诉他如何使用你的 c++ 函数。

    “.pyx”文件的内容通常位于“.pxd”文件中。 “.pyx”文件包含将从 python 调用的函数。示例:

    def hello_world():
        Helloworld()
    

    【讨论】:

    • 我已经尝试过您的解决方案,但恐怕它不能解决我的问题。 C 函数似乎是递归调用的。
    • 我已经解决了,一旦我实现了您的解决方案,原始的 C++ 文件就会被生成的递归文件覆盖。一旦我解决了它就可以了。谢谢!
    【解决方案2】:

    问题有两个方面。一个是,正如 manawy 所说,我没有一个包装 python 函数来调用 C++ 函数。另一个问题是我的 C++ 文件与 .pyx 文件同名,所以当它编译它时,它会递归地覆盖 C++ 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 2014-06-26
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      相关资源
      最近更新 更多