How to pass strings to C++ function from Java using SWIG generated interface

痴心易碎 提交于 2019-12-10 16:47:48

问题


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 start?


回答1:


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).



来源:https://stackoverflow.com/questions/4652638/how-to-pass-strings-to-c-function-from-java-using-swig-generated-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!