[2890] in java-interest

home help back first fref pref prev next nref lref last post

Re: C++ native modules!!

daemon@ATHENA.MIT.EDU (Jason Weiler)
Thu Oct 19 16:30:31 1995

Date: Thu, 19 Oct 1995 10:59:49 -0700
From: weilerj@std.teradyne.com (Jason Weiler)
To: psenthil@iss.nus.sg
Cc: java-interest@java.sun.com

> This is my modified hello.c
> 
> 
> #include <StubPreamble.h>
> #include "Helloworld.h"
> #include <iostream.h>
> void Helloworld_displayHelloworld(struct HHelloworld *t)
> {
>   cout <<"adfsg"<<endl;
>   return;
> }
> 
> I created libHelloworld.so by
> g++    -G -I$JAVAHOME/include -I$JAVAHOME/include/solaris HelloWorld.c hello.C -o
> libHelloworld.so
> 
> On running this  I get
> 
> ld.so.1: /usr2/psenthil/java/bin/../bin/sun4/java: fatal: relocation
> error: symbol not found: Helloworld_displayHelloworld__FPv: referenced
> in /usr2/psenthil/hotjava-psk/native/cpp/libHelloworld.so
> (/usr2/psenthil/hotjava-psk/native/cpp/libHelloworld.so)
> java.lang.UnsatisfiedLinkError no Helloworld in LD_LIBRARY_PATH
> 		  at java.lang.Throwable.<init>(Throwable.java)
> 		at java.lang.Error.<init>(Error.java)
> 		at java.lang.LinkageError.<init>(LinkageError.java)
> 		at java.lang.UnsatisfiedLinkError.<init>(UnsatisfiedLinkError.java)
> 		at java.lang.Runtime.loadLibrary(Runtime.java)
> 		at java.lang.System.loadLibrary(System.java)
> 		at Helloworld.<clinit>(Helloworld.java:6)
> 		at java.lang.UnsatisfiedLinkError displayHelloworld
>       at pgm.main(Helloworld.java:13)
> 
> LD_LIBRARY_PATH=  <is properly set>
> 
> When i changed the cout and iostream to printf and compiled with g++ I
> get the same error. 
> 
> Any help and solutions are welcome.
> 
>  Thanks in advance.
>      Senthil 
> 


I HAVE linked and used C++ dll's with Java from Solaris.  It is certainly
possible with some restrictions.

1) you can't call a member function of a class directly from Java.
2) you can't call different versions of an overloaded function directly from
Java

Here's why:
Java is trying to call the "C-function" name, not the mangled "C++ function"
name.  Notice in your error message above, it says:

> error: symbol not found: Helloworld_displayHelloworld__FPv

The __FPv on the end represents the mangling that C++ uses to differentiate
overloaded functions. (by arguments)  

With this in mind, you CAN still link a C++ dll by simply using the 
extern "C" {}  mechanism.  You must put all your function predeclarations
for functions that will be called directly from Java,  #include "javaString.h",
 #include "StubPreamble.h", and perhaps some other things inside of the
extern "C" {}.

I suggest using the same mechanism that standard C header files use:

#ifdef __cplusplus
extern "C" {
#endif
	.
      stuff
	.
#ifdef __cplusplus
}
#endif

Hope that helps!
-Jason W.
<weilerj@std.teradyne.com>

PS - I should specify that I did this under Alpha 3, and NOT preBeta.
I suspect that it should work just the same becuause it's not as much
a Java thing as it is a C/C++ linking thing.
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post