Generate C # interface from C ++ using SWIG
How can I use SWIG to create a C # interface (or at least a base class with C # layouts) from C ++ using SWIG?
Given:
C ++:
class IWidget
{
public:
virtual void Flob() = 0;
};
class Widget : public IWidget
{
public:
void Flob() {};
};
I would like to output C #:
public interface IWidget
{
void Flob();
}
public class Widget : IWidget
{...}
Note. The solution doesn't need to be an interface, but I need to be able to use mocking frameworks like Moq or Rhino.Mocks to mock the C # Widget class base. My attempts only yielded a generated C # with no public constructors.
source to share
You can get the output you post using one or more of the following:
-
csclassmodifiers
- use
%ignore
in pure virtual methods, add required method declarations with cscode. -
csinterfaces typemap
-
csinterfaces_derived typemap
However, this may not be the easiest way to achieve the goal of making your class mock. Consider expanding your post to clearly define what C # is that you need to achieve this if interface
not used.
Also check out this post: http://swig.10945.n7.nabble.com/Multiple-inheritance-and-C-any-easy-workarounds-available-td11516.html .
Sorry I can't provide the actual answer code, hope the above gets you there.
source to share
Start with
"Interface File" and then "C # Module Creation" is not something in there. Below is the description for compilation / linking in the same document. Do the same in Visual Studio.
$ swig -csharp example.i $ gcc -c -fpic example.c example_wrap.c $ gcc -shared example.o example_wrap.o -o libexample.so $ cscc -o runme *.cs
source to share