TheForbidden
TheForbidden

Reputation: 1573

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?

Upvotes: 3

Views: 1166

Answers (4)

ShujaRafi
ShujaRafi

Reputation: 1

insert the -shared flag

gcc -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux -o libmyhello.so -shared HelloWorld.c

Upvotes: -1

msam
msam

Reputation: 4287

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

Upvotes: 3

user325117
user325117

Reputation:

There are 2 steps to creating an so file:

 cc -c test.c
 cc -shared test.o -o test.so

Have a look at Static, Shared Dynamic and Loadable Linux Libraries for the details.

Upvotes: 2

Xyzk
Xyzk

Reputation: 1342

You need to add main function. add

int main(){
 return 0;
}

, or alternatively int main(char *argv[], int argc)

Upvotes: -2

Related Questions