Search code examples
javaclinuxgccjava-native-interface

Trouble with compiling JNI


I have the following C code implemented by referencing to a header file already generated with JNI:

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"

JNIEXPORT void JNICALL 
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
     printf("Hello World!\n");
     return;
}

When I try to compile it (to generate the so library) using:

cc -g -I/usr/lib/jvm/java-7-openjdk/include
-I/usr/lib/jvm/java-7-openjdk/include/linux HelloWorld.c -o libHelloWorld.so

I got this error:

/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

How can I fix this issue?


Solution

  • you have to add the -shared linker option

    First create the object file:

    cc -c HelloWorld.c
    

    Then create the so

    cc -shared -o libHelloWorld.so HelloWorld.o