8

How do I use SWIG to generate a C# interface (or at least a C# mock-able base class) 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: Solution does not have to be an interface, but I do need to be able to use mocking frameworks such as Moq or Rhino.Mocks to mock out the base of the C# Widget class. My attempts only yielded generated C# with no public constructors.

PatrickV
  • 2,057
  • 23
  • 30
  • Try something after reading the SWIG docs which show clearly how to do this, then update your post with specific problem. – Oliver Sep 09 '14 at 02:19
  • @Schollii Could you point me to the right section? I looked over examples and read C# section, didn't see C# interface mentioned anywhere. – PatrickV Sep 12 '14 at 13:16
  • I think you essentially want an equivalent of http://stackoverflow.com/a/8246375/168175 - although I think that should largely port by doing s/java/cs/ I think there are some smarter options open for C# possibly and some updated bits in latest version of SWIG that I'll investigate later. – Flexo Jan 12 '17 at 18:09

3 Answers3

1

You may be able to get the output you list by using one or more of:

  • csclassmodifiers
  • use %ignore on the pure virtual methods, add necessary method declarations using cscode .
  • csinterfaces typemap
  • csinterfaces_derived typemap

However this may not be the easiest way to achieve the goal of making your class mockable. Consider extending your post to clearly identify what is the c# that you need to achieve this if interface is not used.

Also take a look at this post: http://swig.10945.n7.nabble.com/Multiple-inheritance-and-C-any-easy-workarounds-available-td11516.html.

Sorry I can't provide an actual code answer, hopefully the above gets you there.

Zachi Shtain
  • 826
  • 1
  • 13
  • 31
Oliver
  • 27,510
  • 9
  • 72
  • 103
0

The answer is in the docs more or less. You need to refer to the Java section here.

%module MyWidgetModule
%include <swiginterface.i>

%{
    #include "IWidget.h"
%}

%interface_impl(IWidget); // Use namespace qualified class name
%include "IWidget.h"

This also works when you're using shared pointer support for SWIG. It will generate a class and the intended interface.

user16217248
  • 3,119
  • 19
  • 19
  • 37
-2

Start with the

Swig tutorial

"Interface file" and then "Building a C# module" are the imp things there. Below is mentioned for compile/link 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 
sambha
  • 1,333
  • 3
  • 17
  • 30
  • I think there's confusion on my use of "interface". I know how to use SWIG to output C#, but the C# it outputs is a class, not an interface. If the base C++ is pure abstract, I want the generated C# to be an interface, not a class. – PatrickV Sep 12 '14 at 18:48