call Cython function from C++

前端 未结 3 1387
终归单人心
终归单人心 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:21

    You can achieve that by doing pure cdef functions :

    # declare the prototype of your function
    ctypedef void (*callback_ptr)(int arg)
    
    # declare your function as cdef
    cdef void my_callback(int arg):
        print 'doing some python here', arg
    
    # now, you can use the cdef func as a callback
    # in pure C !
    cdef void run():
        cdef callback_ptr p = my_callback
        p(42)
    
    if __name__ == '__main__':
        run()
    

    Note: you can use "cython -a" to see that they are no python code involved for the content of run. So it will work with your c library.

提交回复
热议问题