This is the mail archive of the java-discuss@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Cygnus Native Interface


I've put together a couple of trivial examples of
how to get CNI to actually compile and link in both the
2.95 and 2.96 worlds.

Consider these to be in the public domain.  I didn't bother
formatting them for inclusion in the FAQ or web site, though
I could do that if desired.

Here is a sample project demonstrating the basics of building
a CNI application with GCJ version 2.95.

It assumes that you have installed the binaries to /usr/local/bin.

Version 2.95 does not install the include files necessary to 
compile CNI programs, so you'll need to reference the source
and build directories to get the relevant files.

This drawback is corrected in version 2.96.

The source directory is /home/gcc/source/libgcj-2.95.1.
The build directory is /home/gcc/build/libgcj

==> Makefile <==
sample:  sample.o sampNat.o
	/usr/local/bin/gcj -o sample sample.o sampNat.o -lstdc++ --main=sample

sample.o:  sample.class
	/usr/local/bin/gcj -c sample.class

sample.class: sample.java
	/usr/local/bin/gcj -C sample.java

sample.h: sample.class
	/usr/local/bin/gcjh sample

sampNat.o: sample.h sampNat.cc
	/usr/local/bin/gcc \
	    -I/home/gcc/source/libgcj-2.95.1/libjava/include \
	    -I/home/gcc/source/libgcj-2.95.1/libjava \
	    -I/home/gcc/build/libgcj/i486-pc-linux-gnu/libjava/include \
	    -I/home/gcc/build/libgcj/i486-pc-linux-gnu/libjava \
	    -fno-rtti \
	    -c sampNat.cc

clean:
	rm -f sample sample.o sampNat.o sample.class sample.h

==> sampNat.cc <==
#include "sample.h"
#include <stream.h>
#include <java/io/PrintStream.h>
#include <java/lang/System.h>
#include <java/lang/String.h>
#include <cni.h>

void sample::myNative(java::lang::String *s)
{
  cout << "Hello, C++" << endl;

  // The following line causes a link error for a missing typeinfo
  // unless this file is compiled with '-fno-rtti'
  java::lang::System::out->println(s);
}

==> 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");
  }
}
Here is a sample project demonstrating the basics of building
a CNI application with GCJ version 2.95.

It assumes that you have installed the binaries in a directory
in your path.

==> 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++ -fno-rtti -c sampNat.cc

clean:
	rm -f sample sample.o sampNat.o sample.class sample.h

==> 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)
{
  cout << "Hello, C++" << endl;

  // The following line causes a link error for a missing typeinfo
  // unless this file is compiled with '-fno-rtti'
  java::lang::System::out->println(s);
}

==> sample.java <==
public class sample {
  public native void myNative(String s);

  public void myJava() {
    String s = "Hello, Java";
    System.out.println(s);
  }

  public static void main(String args[]) {
    sample x = new sample();
    x.myJava();
    x.myNative("Hello, Java (from C++)");
    x.myJava();
  }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]