java - 使用 jna 加载 lib.so

标签 java c++ global-variables jna .so

我实际上是想开一家图书馆。所以(我用 C++ 做了)用 JNA 我的问题是当有静态变量时我无法打开库。但是我必须在我的库中使用 un singleton 所以我正在寻找为什么静态变量不能与 JNA 一起使用来证明它是一个小库

有 .h 文件:

#ifndef UNTITLED1_LIBRARY_H
#define UNTITLED1_LIBRARY_H
#include <iostream>
class library {
 private:
  static char* h;
 public:
  int hello();
};

extern "C" int hello(){
 library lib;
 return lib.hello();
}

#endif

然后是我的 .cpp 文件:

#include "library.h"
#include "ecrire.h"

#include <iostream>

int library::hello() {
 h = (char*)"hello world";
 std::cout<<h<<std::endl;
 return 45;
}

然后是我的java类和接口(interface)

public class Westgard {
static {       
    System.setProperty("jna.library.path","../logic/resources/calculator"); 
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int maj = InterfaceLibWestgard.INSTANCE.hello();
    System.out.println(maj);
}

}


import com.sun.jna.Library;
import com.sun.jna.Native;

public interface InterfaceLibWestgard extends Library {
  int hello();
  static InterfaceLibWestgard INSTANCE = (InterfaceLibWestgard) 
  Native.loadLibrary("../logic/resources/calculator/libuntitled1.so", 
  InterfaceLibWestgard.class);
}

因此,如果我这样尝试,它不会起作用,但是当从 .h 中删除静态时,它会起作用,但没有人知道为什么我一直在寻找,因为 4-5 小时后仍然不知道为什么......

这是我的问题日志:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load 
 library '../logic/resources/calculator/libuntitled1.so': Native library 
(linux-x86-64/../logic/resources/calculator/libuntitled1.so) not found in 
resource path ([file:/opt/java/jdk1.8.0_151/jre/lib/resources.jar, 

最佳答案

你已经声明了 library::h 但没有定义它。你需要添加

char* library::h = 0;

在你的 cpp 文件中。

推测该库要么无法编译,要么正在编译但希望在另一个库中定义这个缺失的符号。

关于java - 使用 jna 加载 lib.so,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50353659/

相关文章:

ios - 在 Swift 中使用条件编译定义全局变量

java - 我们如何安排任务 24 小时执行并在变量中存储 24 小时响应?

java - 什么是 [Android] Java 的 VB.NET 静态关键字?

c++ - 在不复制缓冲区和内存泄漏的情况下返回指向 vector 数据的指针

c++ - Linux TCP/IP 编程中的精确定时保持事件

c - C中需要将全局变量转换为局部变量

javascript - 如何避免 JavaScript 中的全局变量?

java - Android studio 使用代码为按钮设置十六进制颜色值

java - Xamarin searchview OnActionExpandListener

c++ - 是什么破坏了这段代码?