2017-01-17 35 views
0

我有以下文件:錯誤包裹在C++中的HelloWorld當進入蟒蛇與用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, 
... 

這意味着它被稱爲遞歸。

回答

2

我想這是因爲你的模塊是空的,沒有定義任何功能。

Cython不創建包裝。你必須告訴他如何使用你的C++函數。

此外,「.pyx」文件的內容通常位於「.pxd」文件中。 「.pyx」文件包含將從python調用的函數。例如:

def hello_world(): 
    Helloworld() 
+0

我試過你的解決方案,恐怕它不能解決我的問題。 C函數似乎被遞歸調用。 – SomeRandomPhysicist

+1

我已經解決了它,一旦我實現了你的解決方案,原始的C++文件被遞歸生成的文件覆蓋。一旦我解決了它的工作。謝謝! – SomeRandomPhysicist

1

問題是雙重的。其中之一是,正如Manawy所說的那樣,我沒有一個封裝python函數來調用C++函數。另一個問題是我的C++文件與.pyx文件名稱相同,所以當它編譯它時,它會遞歸地覆蓋C++文件。