3

I am trying to wrap a c++ library using cython. The c++ header file say MyFile.h declares a class like this:

class MyClass {

public:
    enum MyEnum{
        TYPE0 = 0,
        TYPE1 = 1,
        TYPE2 = 2,   
    };

    MyClass(MyEnum val=TYPE0){
        // ...
    }
    // ...
}

The pxd file has this:

cdef extern from "<MyFile.h>":

    cdef cppclass MyClass:

        cdef enum MyEnum:
            TYPE0 = 0
            TYPE1 = 1
            TYPE2 = 2

        MyClass(MyEnum val=TYPE0) except +

But cython does not compile it. How do I do this?

1 Answers1

4

Try using namespace:

cdef extern from "MyFile.h" namespace "MyClass":
    cdef enum MyEnum:
        TYPE0 = 0
        TYPE1 = 1
        TYPE2 = 2

Or maybe this will work as well:

cdef extern from "MyFile.h":
    cdef enum MyEnum "MyClass::MyEnum":
        TYPE0 = 0
        ...
Czarek Tomczak
  • 20,079
  • 5
  • 49
  • 56
  • 2
    This is definitely the way to do it. It's probably worth adding that you access this in Python as `MyEnum`, not `MyClass::MyEnum` – DavidW Jul 17 '16 at 14:30