0

Problem I'm trying to solve in shortest example looks like that. I have 2 swig's interface files.

  • first swig file Common.i looks like that:
/* Common.i */
%module Common
%include "Common.hpp"

I generate swig wrappers with command:

swig -c++ -java -package com.wrappers.Common -outdir "wrappers/Common" -o "wrappers_cpp/swig_Common_wrapper.cpp" "Common.i"

Common.hpp has definitions of class A and B, so this swig command above generates files A.java and B.java in catalog wrappers/Common

  • second swig file Example.i looks like that:
/* Example.i */
%module Example

%include "Common.i"

%include "Example.hpp"

and again I generate swig wrappers with command:

swig -c++ -java -package com.wrappers.Example -outdir "wrappers/Example" -o "wrappers_cpp/swig_Example_wrapper.cpp" "Example.i"

And here my problem begins: In catalog wrappers/Example I have duplicate files A.java and B.java, because I included Common.i in Example.i.

I would like to have one copy of those classes above.

Is there a way to avoid this duplication?

AnDevi
  • 67
  • 7
  • Maybe you should not run the first swig command. Since "Common.i" is included in "Example.i", it will be parsed as well at the same time (as if you wrote two modules in a single interface file). That being said, I'm not sure we can define several modules in a single interface file. – Fareanor Jun 20 '23 at 09:26
  • Maybe it's possible to define several modules in a single interface file but it's not a solution for me. I want to include `Common.i` in more than one swig's interface. So again even if I define `Common.i` in `Example.i` and in another file `Example2.i` I will have duplicated `java` files in `Example` and `Example2` catalogs. – AnDevi Jun 20 '23 at 09:41
  • 1
    Try `%import "Common.i"` instead of `%include "Common.i"`. – Mark Tolonen Jun 20 '23 at 13:54
  • From [here](https://www.swig.org/Doc1.3/Modules.html) they use indeed the `%import` directive. – Fareanor Jun 20 '23 at 14:55

1 Answers1

1

Per Working with Modules: Basics, either of the following should work:

%import "Common.i"
%import(module="Common") "Common.hpp"

According to File Imports:

The purpose of %import is to collect certain information from another SWIG interface file or a header file without actually generating any wrapper code.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • It works for files duplications, but now files ganarated from `Example.i` are in different catalog, and also in different package, so `java` files from `Example.i` doesn't know anything about `A.java` and `B.java` because of missing imports. Do I have to add all those imports by myself in swig interface files? – AnDevi Jun 22 '23 at 10:13
  • Another thing is that generated classes in `A.java` and `B.java` have protected constructors and I can only use them if I am in the same package, but I'm not. So another question is if I can force those constructors to be public ? – AnDevi Jun 22 '23 at 11:10
  • @AnDevi Sorry I use SWIG with Python not Java so not sure of the solution. The .i files normally need `%{ #include "Common.hpp" %}` in addition to `%include` to add the headers to the generated wrapper. That could be part of the problem. – Mark Tolonen Jun 22 '23 at 12:49
  • Yeah, I know about `%{ #include "Common.hpp" %}`, but missing imports, are in `java` not `c++`, I can add `java` import in similar way, bet there is still problem with protected constructors in `java`. – AnDevi Jun 22 '23 at 12:53
  • Ok, I found answers to my questions here: https://stackoverflow.com/questions/16499202/swig-importing-generated-class-from-a-different-module-and-package-into-the-curr – AnDevi Jun 23 '23 at 11:12