JNI使用静态NTL库构建共享库 [英] JNI build shared library using static NTL library

查看:103
本文介绍了JNI使用静态NTL库构建共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为JNI编写了C ++代码,然后构建了共享库.该共享库正在使用NTL静态库.但是会出现以下错误.

I wrote C++ code for JNI then built shared library. This shared lib is using NTL static library. But it gets following error.

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

Compiler configuration and error (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

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

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

推荐答案

由于对共享库使用-fPIC标志,因此也需要使用-fPIC标志重新编译NTL.

Because you use -fPIC flag for your shared library, you need to recompile NTL with -fPIC flag too.

这篇关于JNI使用静态NTL库构建共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆