Can I call clojure code from C++?

前端 未结 2 624
有刺的猬
有刺的猬 2021-01-05 12:05

I\'m writing a plugin for a program written in C++.

Plugins are placed in a specific dir and get called by the main application. I would like to write most of the pl

2条回答
  •  醉梦人生
    2021-01-05 12:49

    JNI should be pretty straightforward for this.

    I would approach it this way:

    1. develop your clojure code with a well defined external interface, ie, whatever set of methods/functions you need to invoke.
    2. package it as a standalone uber-jar (containing the clojure libs as well)
    3. write your c++ wrapper, which must do the following:
      • create the jvm with your uberjar on the classpath (see this link: http://java.sun.com/docs/books/jni/html/invoke.html )
      • load your clojure class
      • provide a facade that maps c++ functions to the underlying java methods (clojure functions)

    You can test your uber-jar from step 2 via a simple standalone java test harness that creates the main clojure class and invokes the appropriate methods; this will let you know that you have a good java/clojure jar in case you run into any issues in the jni invocation in step 3.

    As you check the jni references pay particular attention to the slight/subtle calling differences between the c and c++ jni linkages.

    Good luck.

提交回复
热议问题