How to pass strings to C++ function from Java using SWIG generated interface
I have a bunch of C++ functions that take C std:string as function parameters.
I want to pass java Strings to those functions. I have generated a SWIG JNI interface between Java and C++. I can see the no-args constructor fine, but if I try to compile my java with the String arguments in the constructor, I get "cannot find symbol" and I think it is because of something funny going on with the way the class constructor was defined.
How do you remedy this problem? Is a typemap the answer? If so - where do you star开发者_JAVA百科t?
One way, if you have a small set of functions to call, might be to simply wrap the C++ methods that take std::string with methods that take char* and call them from Java with Java Strings since the default type-map between Java String C++ char* will work as expected. You could also add something like this to your interface.i file:
%include "std_string.i" // for std::string type-maps
#include <string>
I think that this will work for your case without any modification. For more information see the Java SWIG default type-map documentation and the std_string.i and std_basic_string.i files found wherever your swig is installed. This may not be available in older versions of SWIG (i am using 2.0.1).
精彩评论