Search code examples
java-native-interface

Undefined reference for JNI methods


I'm trying to use JNI with an existing C library. When I try to generate the .so file using this command:

gcc -shared -fpic -o libWrapper.so -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include/ -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include/linux/ -I/home/testdpdk/packet-pro/dpdk-2.0.0/i686-native-linuxapp-gcc/include Wrapper.c -Wl,-z,defs

I get errors to do with undefined references for the methods which are included with the JNI:

/tmp/ccyrfz1B.o: In function `Java_Wrapper_eal_1init':
Wrapper.c:(.text+0x62): undefined reference to `GetObjectArrayElement'
Wrapper.c:(.text+0x7f): undefined reference to `GetStringUTFChars'
Wrapper.c:(.text+0xce): undefined reference to `env_init'

How would I go about getting the JNI methods references working? Also, how would I link an existing C library to this shared object file for the 'env_init' method (libWrapper.so)?


Solution

  • You appear to be calling GetObjectArrayElement and friends as functions. They are not functions: they are function pointers within env. Call them as

    (*env)->GetObjectArrayElement(env, ...)