Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIG - How to ignore C++ classes I don't need to expose (in Java)?

Tags:

java

c++

swig

Consider a SomeClass.h with the following functions declared

void doSomethingSimple(double);
void dealWithComplexClasses(ComplexClass&);

I want to expose doSomethingSimple(double) in Java, but NOT dealWithComplexClasses(ComplexClass&)

I have tried various things such as:

%ignore SomeClass::dealWithComplexClasses(ComplexClass&);
%ignore SomeClass::dealWithComplexClasses;

I also found this SO question which is answered but did not help me (while the problem is similar so I must be missing something) But in the end Swig always creates a proxy java class for ComplexClass and there is a dealWithComplexClasses(SWIGTYPE_p_ComplexClass class) in the SomeClass.java

How can I tell Swig to generate code only for what I want to expose in Java ? Or to ignore functions and classes I don't want to expose?

like image 958
znat Avatar asked Mar 21 '14 14:03

znat


People also ask

Does SWIG work with C++?

The SWIG parser can handle simple C++ class definitions and supports public inheritance, virtual functions, static functions, constructors and destructors. Currently, C++ translation is performed by politely tranforming C++ code into C code and generating wrappers for the C functions.

What is SWIG in Java?

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.

What is SWIG JNI?

The Java extension to SWIG makes it very easy to plumb in existing C/C++ code for access from Java, as SWIG writes the Java Native Interface (JNI) code for you.


1 Answers

You are doing the right thing. The %ignore SomeClass::dealWithComplexClasses should have worked so I suspect you were trying other things at the same time and interpreted errors as indication that this hadn't worked. Try:

%ignore ComplexClass; // will not wrap ComplexClass
%ignore SomeClass::dealWithComplexClasses; // do not wrap that method or any of its overloads

Another possibility but can't tell from your post, is that your class is in a namespace. In this case you need the namespace prepended.

If it still doesn't work, need more info, post a minimal .i and .h for this.

like image 65
Oliver Avatar answered Sep 30 '22 20:09

Oliver