0

I have the following files:

helloworld.cpp which contains

#include <iostream>                                                     
#include <Python.h>                                                     

void Helloworld(){                                                      
  std::cout << "Hello world!" << "\n";                                  
}

helloworld.pyx which contains:

cdef extern from "helloworld.cpp":                                      
    cpdef void Helloworld() 

and setup.py which contains:

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

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:

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.

SomeRandomPhysicist
  • 1,531
  • 4
  • 19
  • 42

2 Answers2

2

I think it's because your module is empty and does not define any function.

Cython does not create the wrapper. You have to tell him how to use your c++ functions.

Also the content of your ".pyx" file is typically in a ".pxd" file. The ".pyx" file contains the functions that will be called from python. Example :

def hello_world():
    Helloworld()
manawy
  • 36
  • 4
1

The problem was two-fold. One was that, as manawy stated, I didn't have a wrapper python function to call the C++ function. 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.

SomeRandomPhysicist
  • 1,531
  • 4
  • 19
  • 42