我一直在尝试为这个小小的C++类创建SWIG包装,在3小时的大部分时间内都没有成功,所以我希望你们当中的一个可以帮我一个忙。我有以下类(class):
#include <stdio.h>
class Example {
public:
Example();
~Example();
int test();
};
#include "example.h"
随着实现:
Example::Example()
{
printf("Example constructor called\n");
}
Example::~Example()
{
printf("Example destructor called\n");
}
int Example::test()
{
printf("Holy shit, I work!\n");
return 42;
}
我已经通读了介绍页面(www.swig.org/Doc1.3/Java.html)几次,而没有获得对这种情况的大量了解。我的脚步是
example_wrap.cxx(无链接)
下面)
第4步和第5步对我来说造成了很多问题,从基本的(由于不在java的路径中而找不到库“example”)到奇怪的(即使没有将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!");
}
}
好吧,如果有人在这些模糊的包装技术中找到了导航,那么我将比开路更快乐,尤其是如果您可以提供example.i和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
关于java - SWIG:从Plain C++到工作的包装器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2761223/