error while compiling jni.h

M

Marcelo

Hi,

I am getting a very strange error

ubuntu:~/docs/jni/jniexamples/chap2/HelloWorld$ gcc
-I/usr/local/share/jdk1.5.0_05/include
-I/usr/local/share/jdk1.5.0_05/include/linux HelloWorld.c
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../lib/crt1.o: In function
`_start':
.../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status

How can I fix it?

thank you very much,

Marcelo
 
N

Neelesh Bodas

Marcelo said:
Hi,

I am getting a very strange error

ubuntu:~/docs/jni/jniexamples/chap2/HelloWorld$ gcc
-I/usr/local/share/jdk1.5.0_05/include
-I/usr/local/share/jdk1.5.0_05/include/linux HelloWorld.c
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../lib/crt1.o: In function
`_start':
../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status

How can I fix it?

By writing a function called int main() or int main(int , char**)
 
M

Marcelo

HelloWorld.c is:

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

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

---HelloWorld.java ----------
class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}

This is a jni program from the Sun Tutorial.

thanks for your help,.

Marcelo
PS: the command
gcc -I/usr/local/share/jdk1.5.0_05/include
-I/usr/local/share/jdk1.5.0_05/include/linux HelloWorld.c
 
P

Procrastinx

If iam not wrong the .cpp file should be compiled into a shared object
library/DLL. Next the java class using the native function should use
some standard calls to utilize the Shared Object/DLL.

No wonder youe code returns such an error.... how can it produce an
executable if it does not find main .. so compile it into a Shared
Object.
 
P

Procrastinx

Sorry , i was typing ".cpp file" since this list was for cpp.But the
same explanation holds good for C also.
 
P

Procrastinx

Sorry , i was typing ".cpp file" since this group was for cpp.But the
same explanation holds good for C also.
 
M

Marcelo

Procrastinx said:
If iam not wrong the .cpp file should be compiled into a shared object
library/DLL. Next the java class using the native function should use
some standard calls to utilize the Shared Object/DLL.

No wonder youe code returns such an error.... how can it produce an
executable if it does not find main .. so compile it into a Shared
Object.
great idea!!!

with the -shared option it works just fine

thanks alot

Marcelo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top