【问题标题】:JNI build shared library using static NTL libraryJNI 使用静态 NTL 库构建共享库
【发布时间】:2017-01-03 09:23:14
【问题描述】:

我为 JNI 编写了 C++ 代码,然后构建了共享库。此共享库使用 NTL 静态库。但它得到以下错误。

编译器配置和错误(Netbeans):

g++ -c -O2 -s -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux      
-I/usr/local/include/NTL -fPIC  -MMD -MP -MF "build/Debug/GNU-Linux/ntl4java.o.d" 
-o build/Debug/GNU-Linux/ntl4java.o ntl4java.cpp
mkdir -p dist/Debug/GNU-Linux
g++     -o dist/Debug/GNU-Linux/libntl4java.so build/Debug/GNU-Linux/ntl4java.o -lgmp -lntl 
-shared -s -fPIC
/usr/bin/ld: //usr/local/lib/libntl.a(lip.o): relocation R_X86_64_32 against .rodata.str1.1' 
can not be used when making a shared object; recompile with -fPIC
//usr/local/lib/libntl.a: error adding symbols: Bad value

ntl4java.cpp

#include <NTLmethod.h>
#include <NTL/ZZ_pX.h>
#include <string.h>

using namespace std;
using namespace NTL;

JNIEXPORT jstring JNICALL Java_NTLmethod_mult(JNIEnv *env, jclass obj, jstring input) {
    const char *str = env->GetStringUTFChars(input, 0);
    ZZ p;
    char c[128];
    strcpy(c, str);
    env->ReleaseStringUTFChars(input, str);

    return env->NewStringUTF(c);
}
int main(){
   return 0;
}

NTLmethod.h

#include <jni.h>

#ifndef _Included_NTLmethod
#define _Included_NTLmethod
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jstring JNICALL Java_NTLmethod_mult(JNIEnv *, jclass, jstring);

#ifdef __cplusplus
}
#endif
#endif

【问题讨论】:

    标签: c++ java-native-interface ntl


    【解决方案1】:

    因为您为共享库使用了 -fPIC 标志,所以您也需要使用 -fPIC 标志重新编译 NTL。

    【讨论】:

    • @Maxr 我的意思是你需要用 -fPIC 重新编译/usr/local/lib/libntl.a。你没有展示你是如何编译 libntl.a 库的。
    • 好的,我明白了,让我试试这个 :)
    • 是的,您的解决方案解决了我的问题。另外,NTL 使用 GMP 库,所以编译 GMP 也需要这个标志。谢谢
    • @Maxr 如果我的解决方案解决了您的问题,请接受我的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多