When reading C ++ structs in Cython

Since C ++ strcut is a special definition of class, it is better to use cdef cpp class instead of cdef struct. If you use cdef struct when reading a struct that does not inherit or do anything with clang, it will not work (as of August 2015).

I don't know if I read the inherited struct because it is not written anywhere in the cython documentation.

reference

When cdef struct

hoge.h


typedef struct {
    int moke;
    double fuga;
} HogeStruct;

hoge.pxd


cdef extern from "hoge.h" nogil:
    cdef struct HogeStruct:
        int fuga
        double piyo

hoge.pyx



def foo():
    cdef HogeStruct bar


If you do this, ```elaborated type refers to a typedef `will appear in the struct HogeStruct bar in pix. struct HogeStruct __pyx_v_bar; `` is generated, so it conflicts with the definition statement of the struct itself.

use cpp class

Especially if you simply use cppclass instead of struct

hoge.h


typedef struct {
    int moke;
    double fuga;
} HogeStruct;

hoge.pxd


cdef extern from "hoge.h" nogil:
    cdef cppclass HogeStruct:
        int fuga
        double piyo

hoge.pyx



def foo():
    cdef HogeStruct bar

that's all.

Recommended Posts

When reading C ++ structs in Cython
Note on encoding when LANG = C in Python
Use pydantic when reading environment variables in Python
Handle signals in C
Access MongoDB in C
Next Python in C
C API in Python 3
Machine language embedding in C language
Heapsort made in C language
Use regular expressions in C
Imitated Python's Numpy in C #
Binary search in Python / C ++
Attention when os.mkdir in Python
Minimum spanning tree in C #
Speed up processing by using C ++ vector, unordered_map internally in Cython