如何从Java使用SWIG生成的接口传递字符串到C ++函数 [英] How to pass strings to C++ function from Java using SWIG generated interface

查看:418
本文介绍了如何从Java使用SWIG生成的接口传递字符串到C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆C ++函数,以C std:string作为函数参数。

I have a bunch of C++ functions that take C std:string as function parameters.

我想传递java字符串到这些函数。我已经在Java和C ++之间生成了SWIG JNI接口。我可以看到no-args构造函数很好,但是如果我尝试在构造函数中使用String参数编译我的java,我得到can not find symbol,我认为这是因为类的构造函数

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.

如何解决这个问题?是一个typemap是答案吗?如果是这样,你从哪里开始?

How do you remedy this problem? Is a typemap the answer? If so - where do you start?

推荐答案

一种方法是,如果你有一小组函数要调用,只需要包装的std :: string的方法std :: string的方法,采取char *和调用他们从Java与Java字符串,因为Java String C ++ char *之间的默认类型映射将按预期工作。您还可以在interface.i文件中添加类似这样的东西:

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>

我认为这将适用于你的情况,没有任何修改。
有关详细信息,请参见 Java SWIG默认类型映射文档
std_string.i和std_basic_string.i文件找到您的swig安装的地方。这个
可能不适用于旧版本的SWIG(我使用的是2.0.1)。

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

这篇关于如何从Java使用SWIG生成的接口传递字符串到C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆