从Java调用C ++共享库中的函数

时间:2016-06-01 15:26:43

标签: java c++ java-native-interface wrapper swig

我已经实现了一个Java代码,它调用名为math(libmath.so)的C ++共享库。但是,当我尝试调用库实现的函数时,我收到错误:

  

libmath.so:未定义的符号:lt_dlinit

对此有何指导?

Java代码:

public class runme 
{
  static {
    try {
        System.loadLibrary("math");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load\n" + e);
      System.exit(1);
    }
  }

 public static void main(String argv[]) 
 {
    System.out.println( "Hello and welcome to test_program." );

   //assuming that I have NumberVectr filled correctly. 
   NumbersVectr numbers = new NumberVectr(10);
   for (int i=0; i< 10; i++)
   {
      numbers.add((Number)i);  
   }

    //Now, the problem I am having is in invoking the library
    //MathematicsWrapper is a Java wrapper class that wraps the original Mathematics class implemented in C++. 
    //compute is an arbitrary function implemented in the original C++ class, which itself loads another native library once instantiated.
    MathematicsWrapper myMaths = new MathematicsWrapper();
        myMaths.compute(numbers); 
 }
} 

原始的C ++数学课程如下所示:

class MathematicsWrapper 
{
public:

  MathematicsWrapper ()
  {
    load_library("math_core.so");
  };

  ~MathematicsWrapper (){
    unload_library();
  };

  int compute(std::vector<int> &numbers);

我有以下输出:

  

您好,欢迎来到test_program。

     

/ usr / lib / jvm / java-7-openjdk-amd64 / bin / java:符号查找错误:   /home/kevin/test_prgram/src/libmath.so:   未定义的符号:lt_dlinit

MathematicsWrapper定义为:

/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 3.0.9
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */

public class MathematicsWrapper {
  private transient long swigCPtr;
  protected transient boolean swigCMemOwn;

  protected MathematicsWrapper(long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;
  }

  protected static long getCPtr(MathematicsWrapper obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        mathsJNI.delete_MathematicsWrapper(swigCPtr);
      }
      swigCPtr = 0;
    }
  }

  public MathematicsWrapper() {
    this(mathsJNI.new_MathematicsWrapper(), true);
  }

  public int compute() {
    return mathsJNI.MathematicsWrapper_compute(swigCPtr, this, NumbersVectr.getCPtr(numbers), numbers);
  }
}

Swig定义:

%module math_module   
%{
    mathematics.h   //contains C++ header for MathematicsWrapper class
                    //so that Java class for MathematicsWrapper can be genereated
%}


%include "std_vector.i"
%template(NumbersVectr) std::vector<int>;

0 个答案:

没有答案