Newbie question (sorry)

Jeff Sturm jsturm@one-point.com
Thu Feb 22 21:13:00 GMT 2001


On Thu, 22 Feb 2001, Jeff Morrow wrote:
> I hate being the clueless newbie, but I looked all over the existing
> documentation and recent posts and couldn't find anything.  I'm
> wondering if there's any kind of a tutorial on working with large
> projects with gcj.  I have directory tree containing code for a library
> plus a separate directory tree containing an application calling that
> library.  I can't figure out how to build the application.

How did you build the library?  Did you create both a class archive
(jar/zip) and static/shared lib?

> I have gcj 2.96 from the RedHat 7 distribution.  I've gotten as far as
> compiling a HelloWorld.java, and that works.  As soon as I try doing
> more than one file, though, I just crash and burn...

What exactly went wrong?  It helps us to help you if you can supply an
error message, makefile, etc.

> Any pointers appreciated.

There are several ways to do it.  I prefer to use a Makefile
and build all classes and objects in a directory separate from the
sources. Compiling classes first accelerates the build because
dependent sources do not have to be reparsed.  As a side effect, gcj -C
also builds the directory tree I need for the object files.

Here's a simple example I used recently to build the Xerces XML parser.
It's not perfect, and it assumes one class per source file which is
actually wrong for inner classes, but it was sufficient to build the
parser (I used a similar makefile to build the samples):


# Path to Xerces sources
SRCDIR = ../src

# List of sources to compile
SOURCES = \
javax/xml/parsers/DocumentBuilder.java \
javax/xml/parsers/DocumentBuilderFactory.java \
javax/xml/parsers/FactoryConfigurationError.java \
...
...
...
org/xml/sax/SAXParseException.java \
org/xml/sax/XMLFilter.java \
org/xml/sax/XMLReader.java

# Construct lists of classes, objects from SOURCES
CLASSES = ${SOURCES:%.java=%.class}
OBJECTS = ${SOURCES:%.java=%.o}

# ${prefix} used to install gcj
GCCDIR = /opt/gcj
GCJ = $(GCCDIR)/bin/gcj --classpath=.:$(SRCDIR):$(GCCDIR)/share/libgcj.jar
GCJFLAGS = -g -fPIC
JAVAC = $(GCJ) -C
JAR = $(GCCDIR)/bin/fastjar

# Tell make where to look for sources
VPATH = $(SRCDIR)

# Rule to build Java class from source
%.class: %.java
	$(JAVAC) -d . $<

# Rule to build object from Java source
%.o: %.java
	$(GCJ) -c $(GCJFLAGS) $< -o $@

# Default target: build class archive and shared library
all: xerces.jar libxerces.so

xerces.jar: $(CLASSES)
	$(JAR) -cf $@ $(CLASSES)

libxerces.so: $(OBJECTS)
	$(GCJ) -shared $(OBJECTS) -o $@



More information about the Java mailing list