call Cython function from C++

前端 未结 3 1385
终归单人心
终归单人心 2020-12-13 14:33

I have a C++ library that has a Python wrapper (written with SWIG). This library allows executing small user-defined code (a callback), such as element-wise operations on a

3条回答
  •  孤城傲影
    2020-12-13 15:16

    The trick with cython is in using the keyword public

    cdef public double cython_function( double value, double value2 ):
        return value + value2
    

    Then the command cythonize along with will create header that you can include. Alternatively, you can create the header yourself:

    #ifdef __cplusplus {
    extern "C"
    #endif
    
    double cython_function( double value, double value2 );
    
    #ifdef __cplusplus
    }
    #endif
    

    Update:

    Then with a little overlay from Python you can use ctypes's callback mechanism

    func_type = CFUNCTYPE(c_double, c_double, c_double)
    
    your_library.set_callback_function ( func_type(user_modules.cython_function) )
    

提交回复
热议问题