简体   繁体   中英

Java Native Interface

I am quite new to JNI. Now I have a Question that, I have my own simple library in C. i have created both .so and .a library for my c library.

Generated libmymath.so Via following Command

gcc -shared -fPIC -o libmymath.so addnsub.o mulndiv.o

JNI.java

public class JNIPart
{
    public native int addJNI(int a, int b);
    public static void main(String args[])
    {
        JNIPart ja = new JNIPart();
        int answer = ja.addJNI(10, 20);
        System.out.println("Answer = "+ answer);
    }
    static 
    {
        System.loadLibrary("mymath");
    }
}

JNI.c

#include "JNIPart.h"
#include "stdio.h"

JNIEXPORT jint JNICALL Java_JNIPart_addJNI(JNIEnv *env, jobject thisObj, jint a, jint b)
{
    return add(a, b);
}

JNI.h

#include "jni.h"
#include "addnsub.h"
#include "mulndiv.h"

JNIEXPORT jint JNICALL Java_JNIPart_addJNI(JNIEnv *env, jobject thisObj, jint a, jint b); 

Note :- Function add() is part of my library.

Now When i am trying to compile JNI.c, It Works Fine with Following Command

gcc -c JNI.c -I="/home/axit/jdk1.7.0_67/include/"

Path of JDK include is for jni.h

Then I am Trying to Create the Shared Library Which should go to the System.loadLibrary() of Java

gcc -shared -fPIC -o libJNI JNI.o

As Mentioned earlier that, libJNI library will be go to the System.loadLibrary() of JNI.java

Question is :-

1) My libJNI Library Should Contain Both my own libmymath.so and JNI.o. Am i Right ?

2) When I am Trying to Generate the .so which is Combination of libmymath.so and JNI.o. It is giving error to as following

Command :-

gcc -shared -fPIC -o libJNI JNI.o libmymath.so

libJNI :- Library that should go in JNI.java, System.loadLibrary()
JNI.o :- Compiled JNI.c
libmymath.so :- my library that contains basic add, sub, mul, and division function


/usr/bin/ld: JNIPart.o: relocation R_X86_64_PC32 against undefined symbol `add' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status

If helper want more information, i'll be more specific and any help will be appreciated

Help me.

Thanks, Axit Soni

Q: My libJNI Library Should Contain Both my own libmymath.so and JNI.o. Am i Right?

It sounds like your JNI.o calls code in libmymath.so. So yes, you need to specify libmylibmath.so in your link command, and you also need to make sure it's available at runtime.

Q: When I am Trying to Generate the .so which is Combination of libmymath.so and JNI.o. It is giving error undefined symbol 'add' can not be used when making a shared object; recompile with -fPIC undefined symbol 'add' can not be used when making a shared object; recompile with -fPIC

I would run "nm" against libmymath.so and make sure you actually have an "add". Perhaps you inadvertantly compiled one or more functions with C++, and the name is mangled?

It wouldn't hurt to do what the error says, and make sure everything is consistently built with "-fPIC".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM