Question

I want to call a native cpp library that uses fftw3 from java. My java class is the following:

/* FourierGui.java */
public class FourierGui {
    native void fourierFromC();
    static {
        System.loadLibrary("fourier");
    }
    static public void main(String argv[]) {
        FourierGui ft = new FourierGui();
        ft.fourierFromC();
    }
}

My cpp library is

/* fourier.cpp */

#include <jni.h>
#include <stdio.h>
#include <cmath>
#include <complex>
#include "fftw3.h"
#include <stdlib.h>

using namespace std;

JNIEXPORT void JNICALL Java_FourierGui_fourierFromC (JNIEnv * env, jobject jobj) {

    // something that uses fftw3

}

and I create the lybrary with:

g++ -o libfourier.so -shared fourier.cpp -lc

But when I run the java class with

java -Djava.library.path="/path/to/lib/" FourierGui

I get the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/red/Scrivania/JNI/libfourier.so: /path/to/lib/libfourier.so: undefined symbol: fftw_plan_dft_1d
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
    at java.lang.Runtime.loadLibrary0(Runtime.java:849)
    at java.lang.System.loadLibrary(System.java:1088)
    at FourierGui.<clinit>(FourierGui.java:10)

I also try to add the path to libfftw3.so in java.library.path but I get the same error. It seems that it can't find this last library.. (if I compile and run the cpp code by itself it works with no error...)

I know there are java wrappers for fftw3 but I would like to do this by myself

Was it helpful?

Solution

I get it... I have to link the fftw3 library when compiling the cpp file:

g++ -o libfourier.so -shared fourier.cpp -lc -lfftw3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top