tags:

views:

95

answers:

2

Hi everyone. 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. 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. My steps were

  1. Create an example.i file
  2. Compile original alongside example_wrap.cxx (no linking)
  3. link resulting object files together
  4. Create a little java test file (see below)
  5. javac all .java files there and run

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

A: 

Depending on what you are doing, it may be easier to use 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.

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
Appreciated, but I'm fairly locked into Java and the source code is not my own.
duckworthd
A: 

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(). It's not great, but it is slightly better to use System.load() along with the current directory. 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. Is there a better method without actually messing with $LD_LIBRARY_PATH or Windows PATH?

Secondly, it seems ld somehow isn't doing its job properly in linking, so instead I use g++ to link. 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"
duckworthd