• Java 访问 C++ 方法:JavaCPP


      JavaCPP提供了在Java中高效访问本地C++的方法。采用JNI技术实现,支持所有Java实现包括Android系统,Avian 和 RoboVM。

      JavaCPP提供了一系列的Annotation将java代码映射到C++代码,并使用一个可执行的jar包将C++代码转化为可以从JVM内调用的动态链接库文件

      Maven:

      <dependency>

      <groupId>org.bytedeco</groupId>

      <artifactId>javacpp</artifactId>

      <version>0.11</version>

      </dependency>

      复制代码

      使用方法:

      C++:

      #include <string>

      namespace LegacyLibrary {

      class LegacyClass {

      public:

      const std::string& get_property() { return property; }

      void set_property(const std::string& property) { this->property = property; }

      std::string property;

      };

      }

      复制代码

      Java:

      import org.bytedeco.javacpp.*;

      import org.bytedeco.javacpp.annotation.*;

      @Platform(include="LegacyLibrary.h")

      @Namespace("LegacyLibrary")

      public class LegacyLibrary {

      public static class LegacyClass extends Pointer {

      static { Loader.load(); }

      public LegacyClass() { allocate(); }

      private native void allocate();

      // to call the getter and setter functions

      public native @StdString String get_property(); public native void set_property(String property);

      // to access the member variable directly

      public native @StdString String property(); public native void property(String property);

      }

      public static void main(String[] args) {

      // Pointer objects allocated in Java get deallocated once they become unreachable,

      // but C++ destructors can still be called in a timely fashion with Pointer.deallocate()

      LegacyClass l = new LegacyClass();

      l.set_property("Hello World!");

      System.out.println(l.property());

      }

      }

      复制代码

  • 相关阅读:
    你不知道的JavaScript(上)this和对象原型(二)
    hihocoder 1566 皇室成员的名字
    csu 1756: Prime
    csu 1770: 按钮控制彩灯实验
    csu 1898: 复盘拉火车
    csu 1901: 赏赐 OR 灾难
    csu 1909: Perfect Chocolate
    csu 1958: 数字游戏
    symfony2 环境搭建笔记
    php preg_match($p, $str, $match)方法简介
  • 原文地址:https://www.cnblogs.com/anjijiji/p/6265241.html
Copyright © 2020-2023  润新知