简体   繁体   English

Java不会从使用swig创建的.dll中调用函数

[英]Java won't call the functions from the .dll created using swig

I had created the dynamic library for java using swig and cmake for learning purposes. 我已经使用swig和cmake为Java创建了动态库,以进行学习。 I can't call a function in java from the same libary that I created. 我无法从创建的同一个库中调用Java中的函数。 The swig doc told me this is the result of forgeting to compile and link the swig wrapper file to my native libary, but I'm very sure that I did that with cmake build. swig doc告诉我,这是忘记编译并将swig包装器文件链接到本机库的结果,但是我非常确定我使用cmake build做到了。

CMakeList.txt CMakeList.txt

cmake_minimum_required (VERSION 2.6)

FIND_PACKAGE(SWIG REQUIRED)
find_package(Java REQUIRED COMPONENTS Runtime Development)
find_package(JNI REQUIRED)
INCLUDE(${SWIG_USE_FILE})
set(JAVA ${java_include_path} )
INCLUDE_DIRECTORIES(${JAVA} ${JAVA}/win32)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET_SOURCE_FILES_PROPERTIES(hello.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(hello.i PROPERTIES SWIG_FLAGS "-includeall")
SWIG_ADD_MODULE(hello java hello.i  hello.cpp)
SWIG_LINK_LIBRARIES(hello ${Java_LIBRARIES} ${JNI_LIBRARIES} ${CMAKE_CURRENT_SOURCE_DIR})

hello.cpp HELLO.CPP

#include "hello.hpp"

int adding(const int x, const int y)
{

  return y + x;
}

hello.hpp hello.hpp

int adding(const int x, const int y);

hello.i hello.i

 %module hello
 %{
   #include "hello.hpp"
 %}
int adding(const int x, const int y);

Can anyone tell me what I"m doing wrong when I'm creating the dynamic library? Thank you for the assistance. 任何人都可以告诉我在创建动态库时我做错了什么吗?谢谢您的帮助。

The reason why I know this is due to this error message in eclipse 我知道这的原因是由于Eclipse中的此错误消息

Exception in thread "main" java.lang.UnsatisfiedLinkError: hello.helloJNI.adding(II)I
    at hello.helloJNI.adding(Native Method)
    at hello.hello.adding(hello.java:14)
    at hello.Main.main(Main.java:14)

Which is the same kind of error message that the docs talk about. 这就是文档所讨论的错误消息。

The missing symbol in your error message is part of the JNI wrapper, not part of your library itself. 错误消息中缺少的符号是JNI包装器的一部分,而不是库本身的一部分。

Usually this means that you've not called System.loadLibrary() for the native part of the SWIG module, before making the first call into it. 通常,这意味着您在第一次调用SWIG模块的本机部分之前尚未调用System.loadLibrary() Your CMake file looks like you've correctly linked the implementation, so it's not the error case you referred to from the documentation. 您的CMake文件看起来已经正确链接了实现,因此这不是您从文档中引用的错误情况。

As well as manually calling: 以及手动调用:

System.loadLibrary("hello"); // The name of your DLL here

from your main method I like to use the following in my SWIG interface files when I'm targeting Java: 从您的main方法中,我在针对Java时喜欢在SWIG接口文件中使用以下内容:

%pragma(java) jniclasscode=%{
  static {
    try {
        System.loadLibrary("hello"); // The name of your DLL here
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load. \n" + e);
      System.exit(1);
    }
  }
%}

This causes the native library to be loaded automatically before it is needed, which seems most natural to Java programmers. 这将导致本机库在需要之前自动加载,这对于Java程序员来说似乎是最自然的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM