SWIG C# de la API de C existente

I have a pure C library whose headers are generated from a custom IDL in Perl. I'd like to move to SWIG (or another generator) to remove the dependency on Perl. I'd also like the free generation of Python and Ruby wrappers that come with SWIG.

I currently generate a C# wrapper which requires the notion of a parent passed to a constructor:

// Pure C API
b = StructB_Create(a);
// not safe to free a until b is freed

// C# wrapper ctor 
B(ClassA a)
  this.parent = a; // ref to prevent gc of a

Is it easy to use SWIG with an existente pure C API in this way?

preguntado el 22 de septiembre de 13 a las 21:09

1 Respuestas

Even if SWIG can't automatically create the reference count for you, it has good abilities for tweaking the resultant code. (I did something similar with refs in Python). You can customize the resultant C#, or you can enhance the C side of things all from within the SWIG .i file.

Using the .i file you can add methods to classes, or you can add prefix or suffix code to existing methods in the target language. Pretty much anything you might need is catered for. For example, see http://www.swig.org/Doc2.0/CSharp.html#CSharp_extending_proxy_class

The doco is not always easy to follow, but it's worth it in the long run. Especially if you have multiple target languages.

Respondido el 23 de Septiembre de 13 a las 07:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.