Generate C# Interface from C++ using SWIG
Asked Answered
T

3

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.

Tarrel answered 8/9, 2014 at 23:31 Comment(3)
Try something after reading the SWIG docs which show clearly how to do this, then update your post with specific problem.Mose
@Schollii Could you point me to the right section? I looked over examples and read C# section, didn't see C# interface mentioned anywhere.Tarrel
I think you essentially want an equivalent of https://mcmap.net/q/558389/-generating-java-interface-with-swig - 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.Overshoot
M
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.

Mose answered 13/9, 2014 at 12:8 Comment(0)
D
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.

Distracted answered 2/5, 2023 at 13:17 Comment(0)
H
-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 
Horeb answered 12/9, 2014 at 15:45 Comment(1)
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.Tarrel

© 2022 - 2024 — McMap. All rights reserved.