gpt4 book ai didi

java - 使用Java原生接口(interface)时出现 "fatal: relocations remain against allocatable but non-writable sections"如何解决?

转载 作者:行者123 更新时间:2023-12-03 23:01:17 24 4
gpt4 key购买 nike

我正在尝试在 Java 代码中调用 C 函数。我有这个 hava 代码。

public class JavaToC {

public native void helloC();

static {
System.loadLibrary("HelloWorld");
}

public static void main(String[] args) {
new JavaToC().helloC();
}
}

.我编译了它,然后创建了头文件。然后制作以下 HelloWorld.c 文件。

#include <stdio.h>
#include <jni.h>
#include "JavaToC.h"
JNIEXPORT void JNICALL Java_JavaToC_helloC(JNIEnv *env, jobject javaobj)
{
printf("Hello World: From C");
return;
}

我尝试使用“gcc -o libHelloWorld.so -shared -I/usr/java/include -I/usr/java/include/solaris HelloWorld.c -lc”编译它,但它给出了以下结果。

Text relocation remains                     referenced
against symbol offset in file
.rodata (section) 0x9 /var/tmp//cc.GaGrd.o
printf 0xe /var/tmp//cc.GaGrd.o
ld: fatal: relocations remain against allocatable but non-writable sections
collect2: ld returned 1 exit status

我正在使用 Solaris 11,我该如何解决这个问题?

最佳答案

目前我无法在 Solaris 机器上测试这个,但是来自 http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/SPARC-Options.html

-mimpure-text suppresses the “relocations remain against allocatable but non-writable sections” linker error message. However, the necessary relocations will trigger copy-on-write, and the shared object is not actually shared across processes. Instead of using -mimpure-text, you should compile all source code with -fpic or -fPIC.

解决方案似乎是添加-fpic 选项来生成与位置无关的代码。

关于java - 使用Java原生接口(interface)时出现 "fatal: relocations remain against allocatable but non-writable sections"如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18812584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com