SWIG: From Plain C++ to working Wrapper
- by duckworthd
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
Create an example.i file
Compile original alongside
example_wrap.cxx (no linking)
link resulting object files together
Create a little java test file (see
below)
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.