So having a simple code in C++. Having a C++ library with:
class A{
public:
virtual void Call();
virtual void CallCall();
virtual ~A();
};
And a swig file:
%{
#include "A.h"
%}
%include "A.h"
%module(directors="1") TestSWIG;
%feature("director") A;
After calling SWIG generator, including generated C++ and C# files into related projects and rebuilding all projects.
swig.exe -c++ -csharp -namespace TestSWIG -outdir ./Sharp/TestSWIG -o ./TestSWIG.cxx TestSWIG.i
We want a simple C# .Net code to work:
using System;
using TestSWIG;
namespace ASharp {
class Cassa : A{
public override void Call() {
Console.WriteLine("Hello from C#");
}
}
class Program {
private static void Main(string[] args) {
var c = new Cassa();
c.CallCall();
Console.ReadLine();
}
}
}
Yet we see that C++ implementation is the one that gets called
void A::Call() {
std::cout << "Hello from C++ World!" << std::endl;
}
Now the question is: what do I do wrong so that inheritance and virtual functions do not work?
C has a stable ABI (Application Binary Interface) increasing compatibility between different compilers. 8. C is somewhat more efficient than C++ since it doesn't need for Virtual Method Table (VMT) lookups. VMT — It is a mechanism used in programming languages to support dynamic dispatch (or Runtime Method Binding).
To build Python extension modules, SWIG uses a layered approach in which parts of the extension module are defined in C and other parts are defined in Python. The C layer contains low-level wrappers whereas Python code is used to define high-level features.
The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme.
The swig command is used to create wrapper code to connect C and C++ code to scripting languages like Perl, Python, etc. from the definition of the interface. For detailed information on writing those interface definitions please refer to /usr/share/doc/swig- doc/Manual/index.
And the answer was... to look into Swig->Examples!=) The problem was in ordering in a .i
file.
%module(directors="1") TestSWIG; // Module name
// Source code refrence
%{
#include "A.h"
%}
%feature("director") A; // objects to support inheritance
%include "A.h" // main file to parse
and it worked as required!=)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With