I have a C++ project with some .cpp and .h files. One of these .cpp has a function with this signature:
int** verts(int L, int W, int l, int w)
I’d like to use this function in a Java project. Yes, I could rewrite it in Java, but it’s a very very articulated function. It would be great calling this function (and obtain a matrix of integers) from Java.
Any idea?
Please consider that I’m a Java programmer, I’m very noob in C++ 😛
You can write a library which contains this function, and wraps it with
a JNI wrapper, but it’s not trivial. The most important question is
what the
int**really represent, and how they are managed: whether andhow you have to delete them, and how you want to represent them in Java.
Just guessing, I suspect that the pointer points to an array of
int*,each of which points to an array of
int; this is a commonrepresentation for a two dimensional array in C (but not what we’d
normally use in C++). As for whether and how to delete them, this
should be documented by the library which defines
verts. Given that,you’ll first have to declare the function as native in a Java class:
(I’ve preferred to return a flattened, single dimensional array, rather
than an
Integer[][]. It will make the C++ much simpler.)what you’ll have to do is write a function along the following lines:
Except that the above also needs considerably more error checking.
Finally, compile and link the above into a dll with the name you gave to
the
System.loadLibrary()function on the Java side. And make sureyour environment is set up so that Java can find the dll (either through
$LD_LIBRARY_PATH (Unix) or %path% (Windows), or by passing the path by
means of
-Djava.library.path=...on the Java command line).