Java SWIG:来自普通C++;工作包装 我一直在努力为这个小小的C++类创建一个WIG包装器,3个小时的大部分时间都没有成功,所以我希望你们中的一个能借给我一只小手。我有以下课程: #include <stdio.h> class Example { public: Example(); ~Example(); int test(); }; #include "example.h"
我已经阅读了介绍页面(www.swig.org/Doc1.3/Java.html)好几次,但都没有深入了解情况。我的脚步声很轻Java SWIG:来自普通C++;工作包装 我一直在努力为这个小小的C++类创建一个WIG包装器,3个小时的大部分时间都没有成功,所以我希望你们中的一个能借给我一只小手。我有以下课程: #include <stdio.h> class Example { public: Example(); ~Example(); int test(); }; #include "example.h",java,c++,swig,Java,C++,Swig,我已经阅读了介绍页面(www.swig.org/Doc1.3/Java.html)好几次,但都没有深入了解情况。我的脚步声很轻 创建一个示例.i文件 并排编译原文 示例_wrap.cxx(无链接) 将生成的对象文件链接在一起 创建一个小java测试文件(参见 (见下文) javac所有.java文件都在那里运行 步骤4和步骤5给我带来了很多问题,从基本的(由于不在java的路径中,所以找不到库“示例”)到奇怪的(除非LD_library_path设置为某个值,否则找不到库,即使它什么都没有)。我
public class test2 {
static {
String libpath = System.getProperty("java.library.path");
String currentDir = System.getProperty("user.dir");
System.setProperty("java.library.path", currentDir + ":" + libpath);
System.out.println(System.getProperty("java.library.path"));
System.loadLibrary("example");
}
public static void main(String[] args){
System.out.println("It loads!");
}
}
好吧,如果有人已经在包装这片浑浊的水域中航行过,我会非常高兴,如果你能为我指路,特别是如果你能提供示例。我和bash命令可以配合使用。根据你正在做的事情,使用scipy.weave可能会更容易。请参见下面的示例,这是相当不言自明的。我对SWIG的经验是,它工作得很好,但传递变量是一个真正的PITA
import scipy.weave
def convolve( im, filt, reshape ):
height, stride = im.shape
fh,fw = filt.shape
im = im.reshape( height * stride )
filt = filt.reshape( fh*fw )
newIm = numpy.zeros ( (height * stride), numpy.int )
code = """
int sum=0, pos;
int ys=0, fys=0;
for (int y=0; y < (height-(fh/2)); y++) {
for (int x=0; x < (stride-(fw/2)); x++) {
fys=sum=0;
pos=ys+x;
int th = ((height-y) < fh ) ? height-y : fh;
int tw = ((stride-x) < fw ) ? stride-x : fw;
for (int fy=0; fy < th; fy++) {
for (int fx=0; fx < tw; fx++) {
sum+=im[pos+fx]*filt[fys+fx];
}
fys+=fw;
pos+=stride;
}
newIm[ys+x] = sum;
}
ys+=stride;
}
"""
scipy.weave.inline(code,['height','stride','fh','fw','im','filt','newIm'])
if reshape:
return newIm.reshape(height,stride )
else:
return newIm
导入scipy.weave
def卷积(im、过滤、重塑):
高度、步幅=im.shape
fh,fw=过滤形状
im=im.重塑(高度*步幅)
过滤=过滤整形(fh*fw)
newIm=numpy.zeros((高度*步幅),numpy.int)
代码=”“
整数和=0,位置;
int ys=0,fys=0;
对于(整数y=0;y<(高度-(fh/2));y++){
对于(int x=0;x<(步幅-(fw/2));x++){
fys=总和=0;
pos=ys+x;
int-th=((高度-y)
我的问题有两方面;其中一个我理解(并且认为是相当愚蠢的),第二个我不理解,但愿意接受
首先,更改“java.library.path”对使用System.loadLibrary()时java的实际外观没有影响。这不是很好,但是在当前目录中使用System.load()会稍微好一些。不幸的是,必须事先知道库的全名,这将对任何跨平台代码造成严重破坏。为什么Java允许您更改这个系统属性而不影响任何东西,这让我感到困惑。有没有一种更好的方法,不必真正弄乱$LD\u LIBRARY\u PATH或Windows PATH
其次,似乎ld在链接中没有正确地完成它的工作,所以我使用g++进行链接。而不是
ld -G example.o example_wrap.o -o libexample.so
我用
结果文件不再有任何链接问题(为什么?)。注意到这两个问题以及下面的示例。我了解了工作代码:
/* File: example.i */
%module test
%{
#include "example.h"
%}
%include "example.h"
非常感谢,但我对Java非常熟悉,源代码不是我自己的。
g++ example.o example_wrap.o -o libexample.so
/* File: example.i */
%module test
%{
#include "example.h"
%}
%include "example.h"