E
E
evrikom2018-03-23 07:41:01
Java
evrikom, 2018-03-23 07:41:01

swig and java. How to make a simple example work?

Initial data:
example.h:

#ifndef _BRIDJ_EXAMPLE_H
#define _BRIDJ_EXAMPLE_H
namespace nl4j {
  class SomeClass {
    int m_value;
  public:
    SomeClass(int value);
    int someMethod(const char* message);
  void someCppFunction(const char* message);        
        };
}
#endif // _BRIDJ_EXAMPLE_H

example.i
%module example 
%{
#include "example.h"
%}
%include "example.h"

We collect our cpp and generated by swig in so, when trying to create an object from java: Error:
SomeClass cl = new SomeClass(6);
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.mycompany.testswig.exampleJNI.new_SomeClass(I)J
  at com.mycompany.testswig.exampleJNI.new_SomeClass(Native Method)
  at com.mycompany.testswig.SomeClass.<init>(SomeClass.java:38)
  at com.mycompany.testswig.runme.main(runme.java:18)

Trying another option:
example.i
%module example 
%{
#include "example.h"
%}
int someMethod(const char* message);
void someCppFunction(const char* message); 
%include "example.h"

team:
gcc -fPIC -o libexample.so example.cpp example_wrap.cxx -I /usr/lib/jvm/java-8-openjdk-amd64/include/ -I /usr/lib/jvm/java-8-openjdk-amd64/include/linux/ -shared

Generated cxx does not compile:
example_wrap.cxx: In function ‘jint Java_exampleJNI_someMethod(JNIEnv*, jclass, jstring)’:
example_wrap.cxx:252:46: error: ‘someMethod’ was not declared in this scope
   result = (int)someMethod((char const *)arg1);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question