JavaCPP - how to actually generate the C++ JNI wrappers?

3.5k views Asked by At

I'm trying to use JavaCPP to create java bindings for some C++ library. The process has 2 aspects

  • A linux shared library (.so) needs to be built, containing the native entry points (JNIEXPORT). It so happens my library is header-only so I just pass the includes to JavaCPP
  • A java class needs to be generated with methods using the "native" keyword, whose signatures correspond to those on the native side

The Java "properties" file (the part which describes how to build the .so and how to generate the Java file) looks like this:

@Properties(
    //target = "Client",  // NOTE: with this commented, .so gets built; with it enabled, Java class gets written
    value = @Platform(
        includepath = {"jnigen/src/main/cpp/mpf"},
        include = "ClientWrapper.hpp"
    )
)
public class Mpf implements InfoMapper {
    public void map(InfoMap infoMap) {
        infoMap.put(new Info("mpf::ClientWrapper").pointerTypes("Client"));
    }
}

The problem is, I can't for the life of me generate the C++ bindings. I know because the .so doesn't contain the entry points, looking at it with nm -D X.so. If I specify a "target" property the Java class gets outputted, which does look good, but no .so. If I leave out target property, the .so is successfully built, but it has no bindings. I also see it hasn't got them, by passing -nodelete to the java -jar javacpp.jar so it keeps the generated cpp files - they only have marshalling code inside and not my stuff (one class).

The JavaCPP documentation is a nightmare, and the steps are so entagled I can't work it out. Suggestions for alternative libraries are also welcome. Thanks.

1

There are 1 answers

0
Samuel Audet On

The new Mapping Recipes for C/C++ Libraries wiki page should clarify all this, but let me know if there is still anything unclear, and I will provide additional precisions here.

In this case, we could call JavaCPP on Mpf with the @Properties(target="Client", ...) value set, creating a class called Client, so the series of commands would look like this:

$ javac -cp javacpp.jar Mpf.java
$ java -jar javacpp.jar Mpf
$ javac -cp javacpp.jar Client.java
$ java -jar javacpp.jar Client