LINUX 下 JNA 调用 so--正确版

发布时间 2023-09-18 12:10:28作者: 我不想学编丿程

1.编写C++ so库

c++代码:注意加上extern “C”,否则无法找到c++方法。

#include <stdlib.h>
#include <iostream>
using namespace std;
 
extern "C"
{
    void test() {
         cout << "TEST" << endl;
    }
 
    int addTest(int a,int b)
    {
      int c = a + b ;
      return c ;
    } 
}

编译so:g++ -fpic -shared -o libtest.so test.cpp

java

···

package com.zhangsan.demo;

import com.sun.jna.Library;

import com.sun.jna.Native;

public class jnatest1 {
// 继承Library,用于加载库文件

    public interface Clibrary extends Library {
// 加载libhello.so链接库

        Clibrary INSTANTCE = Native.load("test", Clibrary.class);

// 此方法为链接库中的方法

        void test();

        int addTest(int a, int b);

    }

    public static void main(String[] args) {
// 调用



    }

}