?这个东西也太神了吧,一键生成java文件,自己写好c++和配置类就行了。
然后另一个需求是c++三方库,比如<eigen>啥的,这种东西也可以直接加在include path里,感觉无敌了。
首先我们写一个配置类
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
import org.bytedeco.javacpp.tools.*;
@Properties(
value = @Platform(
includepath = {"/path/to/include/","/usr/local/Cellar/eigen/3.4.0/include/eigen3"},
include = {"NativeLibrary.h"}
),
target = "NativeLibrary"
)
public class NativeLibraryConfig implements InfoMapper {
public void map(InfoMap infoMap) {
infoMap.put(new Info("SQUARE").cppTypes("double", "double"));
}
}
再写个头文件
#include <string>
#include <iostream>
#include <Eigen/Dense>
#define SQUARE(x) x * x
using namespace Eigen;
class NativeClass {
public:
const std::string& get_property() { return property; }
void set_property(const std::string& property) { this->property = property; }
std::string property;
double rm(){
MatrixXd m = MatrixXd::Random(3,3);
return m(0,0);
}
};
然后运行这俩命令,就会自动生成java文件和.dylib文件
$ java -jar javacpp.jar NativeLibraryConfig.java
$ java -jar javacpp.jar NativeLibrary.java
然后写个测试类试一试
public class Test {
public static void main(String[] args) {
NativeLibrary.NativeClass l = new NativeLibrary.NativeClass();
System.out.println(l.rm());
}
}
完事儿了就