C Wrapper for C++

前端 未结 4 1327
心在旅途
心在旅途 2020-12-13 15:46

I\'d like to use Pure Data as a prototyping tool for my own library. I found out that Pure Data patches are written in C, but my library is written in C++. So how can I use

4条回答
  •  庸人自扰
    2020-12-13 16:34

    You will need to write wrapper functions for every function which needs to be called. For example:

    // The C++ implementation
    class SomeObj { void func(int); };
    
    extern "C" {
      SomeObj* newSomeObj() {return new SomeObj();}
      void freeSomeObj(SomeObj* obj) {delete obj;}
      void SomeObj_func(SomeObj* obj, int param) {obj->func(param)}
    }
    
    // The C interface
    typedef struct SomeObjHandle SomeObj;
    
    SomeObj* newSomeObj();
    void freeSomeObj(SomeObj* obj);
    void SomeObj_func(SomeObj* obj, int param);
    

    Note this must be C++ code. The extern "C" specifies that the function uses the C naming conventions.

提交回复
热议问题