我有以下配置,我尝试创建一个测试 C 函数,该函数返回一个指向字符串数组的指针,然后使用 SWIG 的 crays.i 和 array_functions 对其进行包装,以便我可以访问 Java 中的数组元素。我不确定 %array_class 或 %array_functions 中哪一个最适合这种情况。此示例是包装返回动态创建数组的 C 函数的构建 block 。
不确定性:
- %array_functions(char, SWIGArrayUtility); - 不确定字符是否正确
- 内联 char *getCharArray() - 不确定 C 函数签名是否正确
- 字符串结果 = getCharArray(); - 字符串返回看起来很奇怪,但这就是 SWIG 生成的
- 不确定 inline char *getCharArray() 是否创建一个具有正确包装结构的数组。
SWIG.i:
%module Test
%{
#include "test.h"
%}
%include <carrays.i>
%array_functions(char, SWIGArrayUtility);
%include "test.h"
%pragma(java) modulecode=%{
public static char[] getCharArrayImpl() {
final int num = numFoo();
char ret[] = new char[num];
String result = getCharArray();
for (int i = 0; i < num; ++i) {
ret[i] = SWIGArrayUtility_getitem(result, i);
}
return ret;
}
%}
内联 header C 函数:
#ifndef TEST_H
#define TEST_H
inline static unsigned short numFoo() {
return 3;
}
inline char *getCharArray(){
static char* foo[3];
foo[0]="ABC";
foo[1]="5CDE";
foo[2]="EEE6";
return foo;
}
#endif
Java 主测试器:
public class TestMain {
public static void main(String[] args) {
System.loadLibrary("TestJni");
char[] test = Test.getCharArrayImpl();
System.out.println("length=" + test.length);
for(int i=0; i < test.length; i++){
System.out.println(test[i]);
}
}
}
Java 主测试器输出:
length=3
?
?
,
SWIG 生成的 Java API:
public class Test {
public static String new_SWIGArrayUtility(int nelements) {
return TestJNI.new_SWIGArrayUtility(nelements);
}
public static void delete_SWIGArrayUtility(String ary) {
TestJNI.delete_SWIGArrayUtility(ary);
}
public static char SWIGArrayUtility_getitem(String ary, int index) {
return TestJNI.SWIGArrayUtility_getitem(ary, index);
}
public static void SWIGArrayUtility_setitem(String ary, int index, char value) {
TestJNI.SWIGArrayUtility_setitem(ary, index, value);
}
public static int numFoo() {
return TestJNI.numFoo();
}
public static String getCharArray() {
return TestJNI.getCharArray();
}
public static char[] getCharArrayImpl() {
final int num = numFoo();
char ret[] = new char[num];
String result = getCharArray();
System.out.println("result=" + result);
for (int i = 0; i < num; ++i) {
ret[i] = SWIGArrayUtility_getitem(result, i);
System.out.println("ret[" + i + "]=" + ret[i]);
}
return ret;
}
}
最佳答案
需要进行两项更改:
<char * getCharArray()
必须是char ** getCharArray()
,因为该函数返回一个指向的指针数组(C 指针)字符*
。进行此更改后,会出现一个新的SWIGTYPE_p_p_char
Java 类,要获得它,必须将%include "various.i"
添加到接口(interface)文件中。%array_functions(char, SWIGArrayUtility)
必须为%array_functions(char *, SWIGArrayUtility)
,因为该数组包含指向char *
(String
Java 类)。
我已经使用此包含文件测试了给定的解决方案:
#ifndef TEST2_H
#define TEST2_H
unsigned short numFoo() {
return 3;
}
char ** getCharArray(){
static char* foo[3];
foo[0]="ABC";
foo[1]="5CDE";
foo[2]="EEE6";
return foo;
}
#endif
该接口(interface)文件:
%module Test2
%{
#include "test2.h"
%}
%include "test2.h"
%include "various.i"
%include "carrays.i"
%array_functions(char *, SWIGArrayUtility);
%pragma(java) modulecode=%{
public static String[] getCharArrayImpl() {
final int num = numFoo();
String ret[] = new String[num];
SWIGTYPE_p_p_char result = getCharArray();
for (int i = 0; i < num; ++i) {
ret[i] = SWIGArrayUtility_getitem(result, i);
}
return ret;
}
%}
这个测试类:
public static void main(String[] args) {
System.loadLibrary("test2");
String res[] = Test2.getCharArrayImpl();
System.out.println("length=" + res.length);
for(int i=0; i < res.length; i++){
System.out.println(res[i]);
}
}
关于java - SWIG 和 Java 使用 carrays.i 和 array_functions 表示 C 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9619623/