First of all, I am new to JNI. I have a java class that imports native function, here's Java
code:
public static native byte[] sha512(byte[] message);
static {
System.loadLibrary("sha512");
}
And when I try to call the function in java I got following error:
java: symbol lookup error: /home/kgb/IdeaProjects/JavaCiphersLib/out/production/JavaCiphersLib/libsha512.so: undefined symbol: _Znam
Here's C++
header file which is generated using javah
:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class my_SHA512 */
#ifndef _Included_my_SHA512
#define _Included_my_SHA512
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: my_SHA512
* Method: sha512
* Signature: ([B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_my_SHA512_sha512
(JNIEnv *, jclass, jbyteArray);
#ifdef __cplusplus
}
#endif
#endif
Here's implementation:
#include <jni.h>
#include "my_SHA512.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jbyteArray JNICALL Java_my_SHA512_sha512(JNIEnv* env, jclass _class, jbyteArray message)
{
unsigned char* msg = new unsigned char[5];
delete msg;
printf("Test Test Test\n");
return message;
}
#ifdef __cplusplus
}
#endif
Now, if I remove unsigned char* msg = new unsigned char[5];
line the error's gone. Here's what c++filt
says:
kgb@KGB-PC:~$ c++filt -n _Znam
operator new[](unsigned long)
Here's how I compiled the c++ code:
gcc -I$JAVA_INC_PATH -I$JAVA_INC_PATH./linux -fPIC -c my_SHA512Impl.cpp
gcc -shared -Wl,-soname,libsha512.so.2 -o libsha512.so.2.0 my_SHA512Impl.o
So how I get rid of the error? And it'd be great if you give some explanation of how all that c++ magic works and how to avoid such errors in the future.
Thanks in advance.
UPD Don't know it's useful, but here's ldd libsha512.so
output:
linux-vdso.so.1 => (0x00007ffd29171000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0e1cd86000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0e1d34d000)
有时候使用g ++代替gcc有时会有所帮助,因为默认情况下它会添加C ++运行时库。
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.