Will cni be the sane native interface to c++ in openjdk U gcj?
Glenn Chambers
gchamber@bright.net
Tue Dec 25 15:59:00 GMT 2007
On Tue, 2007年12月25日 at 12:03 +0000, Andrew Haley wrote:
> Glenn Chambers writes:
> > It's been a few years since I last played seriously in this area, but at
> > that point, there were enough impedance mis-matches between C++ objects
> > and Java objects that C-style glue layers turned out to be necessary.
> >
> > In particular, code that threw or caught C++ exceptions couldn't also
> > throw and catch Java exceptions. This may have changed in recent
> > versions; I'll let the developers clarify that point.
>> This works fine, and has for a long while. I don't know how long ago
> you tried.
It was back in the early days of the 3.x series, as you can see from
the FAQ entry I wrote on this subject. (Check out entry 6.1)
If I can get some spare cycles, I'll have to update that example; the
problems with exception personalities are gone in the version 3.3 gcj
on my (quite old) SUSE install.
Replacement text for that example:
==> Makefile <==
sample: sample.o sampNat.o
gcj -o sample \
sample.o sampNat.o -lstdc++ --main=sample
sample.o: sample.class
gcj -c sample.class
sample.class: sample.java
gcj -C sample.java
sample.h: sample.class
gcjh sample
sampNat.o: sample.h sampNat.cc
g++ -c sampNat.cc
clean:
rm -f sample sample.o sampNat.o sample.class sample.h
==> sample.java <==
public class sample {
public native void myNative(String s);
public void myJava(String s)
{
s = s + ", Java";
System.out.println(s);
}
public static void main(String args[]) {
sample x = new sample();
x.myJava("Hello");
x.myNative("Hello, Java (from C++)");
x.myJava("Goodbye");
}
}
==> sampNat.cc <==
#include <gcj/cni.h>
#include "sample.h"
#include <stream.h>
#include <java/io/PrintStream.h>
#include <java/lang/System.h>
#include <java/lang/String.h>
void sample::myNative(java::lang::String *s)
{
// We can call the Standard Library
cout << "Hello, C++" << endl;
// We can also call Java library functions.
java::lang::System::out->println(s);
}
More information about the Java
mailing list