2

I'm trying to call a C function inside a Java code. I have this hava code.

public class JavaToC {

    public native void helloC();

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

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

. I compiled it and then created header file. Then make the following HelloWorld.c file.

#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;
}

I tried compiling this using "gcc -o libHelloWorld.so -shared -I/usr/java/include -I/usr/java/include/solaris HelloWorld.c -lc", but it gives the following result.

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

I'm working on Solaris 11, how can I solve this?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Chamila Wijayarathna
  • 1,815
  • 5
  • 30
  • 54
  • 2
    I have added a Solaris tag because I feel this might be relevant. Feel free to remove it if you disagree. – us2012 Sep 15 '13 at 13:00

1 Answers1

6

I cannot test this on a Solaris machine at the moment, but from 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.

the solution seems to be to add the -fpic option to generate position-independent code.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks you very, very much!! After a few days trying to compile mod_perl for Apache in Solaris 10 with your help I have succeeded at last :-) – Ciges May 27 '14 at 11:18