Brent Faust
Brent Faust

Reputation: 9319

JavaCpp: How to Specify the Native Library?

Where does JavaCpp look for the native library libmynativelib.so when it creates the jni library, /linux-x86_64/libjnimynativelib.so?

JavaCpp is told about the C++ header and shared library using the @Platform annotation like this:

@Platform(include={"MyLibraryHeader.h"}, link = "mynativelib")
@Namespace("mynamespace")
public class MyLibrary {
  ...
}

The above Java class is then compiled and run through JavaCpp like this:

javac -cp javacpp.jar MyLibrary.java
java -jar javacpp.jar -cp ../..   # classpath is parent of com/mypackage dir

Then JavaCpp output:

Generating /<projpath>/jniMyLibrary.cpp
Compiling /<projpath>/linux-x86_64/libjniMyLibrary.so
g++ -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/lib/jvm/java-7-openjdk-amd64/include/linux <path>/jniMyLibrary.cpp -Wl,-rpath,$ORIGIN/ -march=x86-64 -m64 -Wall -O3 -fPIC -shared -s -o /<projpath>/linux-x86_64/libjnimynativelibrary.so -lmynativelib

Which gives this error:

/usr/bin/ld: cannot find -lmynativelib

g++ is not finding libmynativelib.so either in the current dir () or in the linux-x86_64 subdir.

LD_LIBRARY_PATH=<projdir> doesn't help.

What is the recommended way to tell JavaCpp which native library to load?

Upvotes: 2

Views: 1659

Answers (1)

Brent Faust
Brent Faust

Reputation: 9319

The @Platform(link=) annotation already specifies the native library for JavaCpp to link to:

@Platform(include="MyLibraryHeader.h", link="mynativelib")

So all that remains is to tell g++ where to find the library libmynativelib.so. This is done by sending the -L parameter to g++ through the JavaCpp Xcompiler directive:

java -jar javacpp.jar -cp ... Xcompiler -L<libdir>

Upvotes: 1

Related Questions