springboot 调用 c++生成的so库文件
一、创建c文件
SoTest.h
#pragma once
class SoTest
{
int Add(int a,int b);
};
SoTest.cpp
#include "SoTest.h"
int SoTest::Add(int a, int b) {
return a + b;
}
二、创建so文件
/home/ubuntu/projects/SoTest/bin/x64/Debug/libSoTest.so
三、java代码
Maven依赖
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.9.0</version>
</dependency>
核心代码
public class SOTest {
public interface SoTest extends Library {
static SoTest INSTANCE = Native.load("/home/ubuntu/projects/SoTest/bin/x64/Debug/libSoTest.so", SoTest.class);
int _ZN6SoTest3AddEii(String x, int a, int b); // c方法
}
public int add(int a, int b) {
return SoTest.INSTANCE._ZN6SoTest3AddEii("", a, b);
}
}
测试关键代码
public static void main(String[] args){
SOTest soTest = new SOTest();
soTest.add(1,1);
}