简体   繁体   中英

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?

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

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.

插入-shared标志

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

You need to add main function. add

int main(){
 return 0;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM