简体   繁体   English

SWIG:从普通的C ++到工作的Wrapper

[英]SWIG: From Plain C++ to working Wrapper

I've been trying to create a SWIG wrapper for this tiny little C++ class for the better part of 3 hours with no success, so I was hoping one of you out there could lend me a small hand. 我一直在努力为这个小小的C ++课程创建一个SWIG包装器,在3个小时的大部分时间内没有成功,所以我希望你们中的一个人可以借给我一个小手。 I have the following class: 我有以下课程:

#include <stdio.h>

class Example {
public:
    Example();
    ~Example();
    int test();
};

#include "example.h"

Along with the implementation: 随着实施:

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;
}

I've read through the introduction page ( www.swig.org/Doc1.3/Java.html ) a few times without gaining a whole lot of insight into the situation. 我已经阅读了介绍页面(www.swig.org/Doc1.3/Java.html)几次,但没有深入了解情况。 My steps were 我的步骤是

  1. Create an example.i file 创建example.i文件
  2. Compile original alongside example_wrap.cxx (no linking) 与example_wrap.cxx一起编译原始文件(无链接)
  3. link resulting object files together 将结果对象文件链接在一起
  4. Create a little java test file (see below) 创建一个小的java测试文件(见下文)
  5. javac all .java files there and run javac所有.java文件都在那里运行

Well steps 4 and 5 have created a host of problems for me, starting with the basic ( library 'example' not found due to not being in java's path ) to the weird ( library not found even unless LD_LIBRARY_PATH is set to something, even if it's nothing at all). 好了,第4步和第5步给我带来了很多问题,从基本的(库'示例'因为不在java的路径中找不到)开始到奇怪的(即使除非LD_LIBRARY_PATH设置为某些东西,也找不到库,即使它什么都没有。 I've included my little testing code below 我在下面列出了我的小测试代码

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!");
    }
}

Well, if anyone has navigated these murky waters of wrapping, I could not be happier than if you could light the way, particularly if you could provide the example.i and bash commands to go along with it. 好吧,如果有人在这些浑浊的包裹水域中航行,我就不会比你能照亮的方式更开心,特别是如果你能提供example.i和bash命令来配合它。

Depending on what you are doing, it may be easier to use scipy.weave. 根据你正在做的事情,使用scipy.weave可能更容易。 See below for an example, it is fairly self explanatory. 请参阅下面的示例,它是相当自我解释的。 My experience with SWIG is that it works great, but passing around variables is a real PITA. 我对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

My problems stem two-fold; 我的问题是双重的; One of which I understand (and think is rather dumb), and the second which I don't but am willing to go with 其中一个我理解(并且认为相当愚蠢),第二个我不愿但愿意和他一起去

First, changing "java.library.path" has no effect on where java actually looks when using System.loadLibrary(). 首先,更改“java.library.path”对使用System.loadLibrary()时java实际看起来的位置没有影响。 It's not great, but it is slightly better to use System.load() along with the current directory. 它不是很好,但使用System.load()和当前目录稍微好一些。 Unfortunately, the full name of the library must be known beforehand, which will wreak havoc with any cross-platform code. 不幸的是,必须事先知道库的全名,这将对任何跨平台代码造成严重破坏。 Why Java allows you to change this System Property without actually affecting anything befuddles me. 为什么Java允许您更改此系统属性而不会实际影响我的任何困惑。 Is there a better method without actually messing with $LD_LIBRARY_PATH or Windows PATH? 有没有更好的方法,而没有实际搞乱$ LD_LIBRARY_PATH或Windows PATH?

Secondly, it seems ld somehow isn't doing its job properly in linking, so instead I use g++ to link. 其次,似乎ld不知何故在链接中没有正常工作,所以我使用g ++来链接。 Ie, instead of 即,而不是

ld -G example.o example_wrap.o -o libexample.so

I use 我用

g++ example.o example_wrap.o -o libexample.so

The resulting file no longer has any link problems (why?). 生成的文件不再有任何链接问题(为什么?)。 Noticing both of these along with the following example.i got me through to working code: 注意这两个以及下面的例子。我让我通过工作代码:

/* File: example.i */
%module test
%{
#include "example.h"
%}

%include "example.h"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM