Thread View: comp.lang.java.announce
1 messages
1 total messages
Started by Harley Davis
Wed, 10 Jul 1996 00:00
dev/misc/c++java - How to integrate C++ and Java code
Author: Harley Davis
Date: Wed, 10 Jul 1996 00:00
Date: Wed, 10 Jul 1996 00:00
235 lines
7157 bytes
7157 bytes
SUBJECT dev/misc/c++java - How to integrate C++ and Java code URL ftp://ftp.ilog.com/pub/tarfiles/c++java/example.tar.gz (Native) DESCRIPTION In conjunction with a tutorial I'm giving on 8/7/96 at Java Expo in New York, I've written a complete, extended, commented sample showing how to access a C++ library from a Java application using the original library's API. Here it is for everyone's benefit. PLATFORM Sparc hardware running Solaris 2.4 or above, with JDK 1.0.2 and Sun C++ 4.1. SOURCE Included AUTHOR Harley Davis davis@ilog.com CONTACT Harley Davis Ilog Inc. http://www.ilog.com/ info@ilog.com (415) 390-9000 BODY Next month at the SIGS Object Expo/Java Expo I'm giving a tutorial on using C++ and Java together. I wrote an extended example to illustrate how this can be done effectively. I thought other people might be interested in this sample, so I'm making it available via FTP. Here is the README file from the example: Integrating C++ and Java ======================== Introduction ------------ This directory contains the sample code which accompanies the presentation "Java and C++: Getting the Best of Both Worlds" by Harley Davis. What's Included --------------- This sample includes: * A small, contrived C++ library with a hierarchy of shapes and some methods to manipulate them. * Java code implementing the API of the C++ library in a parallel class hierarchy. * C++ stub files which forge the link between the Java API and the C++ library. * A tiny application written in both C++ and Java to compare the two. To run the C++ app, type 'app'. To run the Java app, make sure the JDK is correctly installed and then type 'japp'. Configuration ------------- This sample was written for the following configuration: * Solaris 2.4 or above * Sparc hardware * Sun JDK 1.0.2 * Sun C++ 4.1 Before using this sample, make sure that: * The JDK 1.0.2 is installed and JAVA_HOME is correctly set. * CLASSPATH contains the directory '.' or the directory in which you detar the sample. * LD_LIBRARY_PATH contains the directory '.' or the directory in which you detar the sample. To download and install the library, follow these steps: $ ftp ftp.ilog.com user: anonymous password: <your email> ftp> cd pub/tarfiles/c++java ftp> bin ftp> get example.tar.gz ftp> quit $ gzcat example.tar.gz | tar xvf - $ cd example $ app .... $ java japp .... Library API ----------- The library defines the following class hierarchy (showing the constructor arguments): Shape() Circle(int radius) Square(int height) Triangle(int side) Methods on shapes include: * void draw() Draws the shape. * int height() Returns the height of the bounding box around the shape. * int width() Returns the width of the bounding box around the shape. * void move(int dx, int dy) Moves the shape dx units to the right and dy units down. In addition, circles have a public data member 'int radius'. The Java class hierarchy has an identical API, except that the data member 'radius' for Circle is mapped into a method with two signatures: * int radius() Returns the radius. * void radius(int new_radius) Sets the radius. Strategy -------- Here is how I created the Java binding for the sample library: * The class CppPeer defines a generic superclass for all C++ classes made visible in Java. It contains a slot for storing the C++ object pointer corresponding to the Java peer and a static method called '_load' for loading the shared library corresponding to the class's stubs. * For each class in the C++ library, create a Java class which extends CppPeer if it has no base class in C++, or the homonymous Java class if it does have a C++ base class. * For each signature of each method of each class, make a Java method. If the C++ method is not overloaded, make this method native. If the C++ method is overloaded, make another method for each signature with a unique name (prepended by 'native_' and suffixed by a list of the types of the arguments) which is declared 'native private final'. Then make a regular Java method signature for the actual method name which calls this native method. This is necessary because javah does not allow overloaded native methods. * Each class has to have a static section which loads a shared library implementing the stubs for that class by calling CppPeer's static method '_load'. * Use 'javah' and 'javah -stubs' to generate stubs for these Java classes. * For a class foo, write a C++ file called 'foo_cc.cc' which implements the stubs generated by javah. It does this by calling out to the original C++ library. It includes the javah-generated stubs and surrounds everything with 'extern "C"' to avoid link problems. * Compile each of these into a shared library called libfoo.so. Each must be linked with the original library, which must be a shared library so Java can load it dynamically. Files ----- The files in this sample are: * README This file. * Makefile Makefile to remake everything. * sample.h The interface file for the C++ library * sample.cc The implementation file for the C++ library * libsample.so The compiled C++ library. * app.cc The source code for the C++ application using the library. * app The compiled C++ application. * japp.java The source code for the Java application using the library. * japp.class The compiled Java application. * CppPeer.java Superclass containing generic definition of a C++ peer class in Java. * *.java Where '*' is one of the classes in the library. The source code of the Java class corresponding to *. * *.class Where '*' is one of the classes in the library. The compiled Java class corresponding to *. * *.h Where '*' is one of the classes in the library. The javah-generated header stubs for *'s native methods. * *.c Where '*' is one of the classes in the library. The javah-generated C stubs for *'s native methods. * *.o Where '*' is one of the classes in the library. The compiled javah-generated C stubs for *. * *_cc.cc Where '*' is one of the classes in the library. The hand-written C++ stubs for *'s native methods which call the original library. * lib*.so Where '*' is one of the classes in the library. The shared library for * which is loaded when * itself is loaded by the JVM. All handwritten files are stored under RCS in the RCS directory. Remaking -------- To remake the whole shebang, do the following: $ make clean $ make Conclusion ---------- Good luck! Please free to send any comments and enhancements (such as a Windows port) to <URL:mailto:davis@ilog.com>. -- Harley Davis Director of Technology, Ilog Inc. ------------------------------------------------------------------- Harley Davis net: davis@ilog.com Ilog, Inc. tel: (415) 944-7130 1901 Landings Dr. fax: (415) 390-0946 Mountain View, CA, 94043 url: http://www.ilog.com/ -- To submit, mail postings to java@relog.ch (please read the guidelines posted)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads