Wrap C++ lib with Cython

前端 未结 3 877
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 12:37

I\'m new to Cython and I\'m trying to use Cython to wrap a C/C++ static library. I made a simple example as follow.

Test.h:

#ifndef          


        
3条回答
  •  庸人自扰
    2020-12-23 12:54

    Your Test.pyx file isn't doing what you expect. The print add(2,3) line will not call the add() C++ function; you have to explicitly create a wrapper function to do that. Cython doesn't create wrappers for you automatically.

    Something like this is probably what you want:

    cdef extern from "test.h":
            int _add "add"(int a,int b)
            int _multiply "multiply"(int a,int b)
    
    def add(a, b):
        return _add(a, b)
    
    def multiply(a, b):
        return _multiply(a, b)
    
    print add(2, 3)
    

    You can look at Cython's documentation for more details.

提交回复
热议问题