SWIG: From Simple C ++ to Working Wrapper

I tried to create a SWIG shell for this tiny little C ++ class for most of 3 hours without success, so I was hoping one of you could give 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 read the introduction page (www.swig.org/Doc1.3/Java.html) several times without gaining much understanding of the situation. My steps were

  • Create example.i file
  • Compile the original next to example_wrap.cxx (no links)
  • link the resulting object files together
  • Create a small java test file (see below)
  • javac all .java files there and run

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

, - , , , example.i bash, .

+3
2

, , 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
+1

; ( , ), , ,

-, "java.library.path" , Java System.loadLibrary(). , System.load() . , , - . Java , . , $LD_LIBRARY_PATH Windows PATH?

-, , ld - , g++ . ..

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

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

(?). , . :

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

%include "example.h"
+1

Source: https://habr.com/ru/post/1743893/


All Articles