Can I compile ANTLR to a binary executable (with GCJ?)?

Helge Hess

  1. get & install gcj and libgcj. Both can be found at:

    gcj is part of the GNU compiler (gcc 2.95.1) and libgcj is a separate library containing the Java runtime system.

  2. delete AWT dependend code (antlr/debug/misc directory)
  3. goto antlr root directory
  4. make the libantlr.so library

    Note that you should compile class files, ICE's may occur when compiling the .java source files (I suppose because of Java 1.1 language features)

    First make a shared library containing antlr classes:

           gcj -shared -fPIC -o libantlr.so  
               `find antlr -name "*.class"`

    This generates a lot of output which can be ignored and hopefully a libantlr.so which can be copied into the LD_LIBRARY_PATH, eg to /usr/local/lib.

    On my Linux host the result is a 2MB shared library which can be stripped ('strip libantlr.so') down to 1.1MB. It may be further reduced by selective compilation of only necessary classes ...

  5. make the cantlr binary
           gcj --main=antlr.Tool antlr/Tool.class 
               -o cantlr 
               -L/usr/local/lib -lantlr

    This should result in a small binary (56K on my host) which just loads the libantlr.so. You can copy the binary to /usr/local/bin.

  6. go to examples/java and test cantlr
         cd examples/java
         ../../cantlr java.g

    it should take less than 1s on a P3 host

How to compile antlr generated source with gcj

  1. make libantlr.so as described above
  2. generate your Java stuff as usual, then compile using gcj and libantlr, eg:
           cantlr Grammar.g
           gcj -c -o Lexer.o  Lexer.java
           gcj -c -o Parser.o Parser.java
           gcj -c -o Tokens.o Tokens.java
           gcj -c -o Main.o   Main.java
           gcj --main=Main -o Main Main.o Lexer.o Parser.o Tokens.o -lantlr