简体   繁体   中英

Using multiple dlls with SWIG to C#

I have 2 native c++ dlls, say A and B. Class X lives in A.dll, and class Y lives in B.dll, with an interface that requires a shared_ptr<X> . I'd like to instantiate X and hand it over to Y using this interface, within a C# app.

Because these two types live in different dlls, I build them separately using swig -dllimport A -c++ -csharp Ai , and swig -dllimport B -c++ -csharp Bi . Within Ai, I've used the %shared_ptr(X) macro allowing me to use an object of type X fluidly in all interfaces that require shared_ptr<X> (provided those interfaces are in A.dll).

My problem is, what if class Y in B.dll takes a shared_ptr<X> in it's interface? How can I make that second swig build, with Bi, aware of shared_ptr<X> (which lives in A.dll)?

What you're looking for is %import in SWIG. You can use this as follows, for example with a module test1:

%module test1

%include <std_shared_ptr.i>

%shared_ptr(Foo);

%inline %{
  struct Foo {};
  std::shared_ptr<Foo> foo() {
    return std::make_shared<Foo>();
  }
%}

Which can be referenced from another module, test2:

%module test2

%import "test1.i"

%inline %{
  void bar(std::shared_ptr<Foo>) {
  }
%}

What %import does (in an nutshell) is to read the interface, but not generate any wrapper code for it. That is to say it will be aware of the contents of it but not directly output any thing into the wrapper because of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM