javagcj

How can I compile files separately with gcj?


I use gcj (Cygwin version) to compile 2 java files

$ gcj --version gcj (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Here's my 2 java files:

$ cat MyClass.java

public class MyClass {  
   public MyClass() {  
      System.out.println("Hello, MyClass");  
   }  
} 

$ cat HelloWorld.java

public class HelloWorld {  
   public static void main(String[] args) {  
      MyClass myclass = new MyClass();  
      System.out.println("Hello, World");  
   }   
}

I'm able to execute the code in HelloWorld with

$ gcj.exe -o hello --main=HelloWorld --classpath=. HelloWorld.java MyClass.java

I would like to compile them separatly like

gcj.exe -C MyClass.java

then

gcj.exe  --main=HelloWorld --classpath=. HelloWorld.java

The problem is that I get

/tmp/ccKwa7F4.o: In function '_ZN10HelloWorld4mainEP6JArrayIPN4java4lang6StringEE':  
/home/lucm/HelloWorld.java:3: undefined reference to `MyClass::class$'  
/home/lucm/HelloWorld.java:3: undefined reference to `MyClass::MyClass()'  
collect2: ld returned 1 exit status

Solution

  • When compiling to .o, the linkage model is basically C-like. You have to compile all the files. So what you would do is compile each to a .o and link the results:

    gcj -c MyClass.java
    gcj -c HelloWorld.java
    gcj --main=HelloWorld MyClass.o HelloWorld.o
    

    There are some exceptions to this rule with later versions of gcj, when the binary compatibility ABI was introduced. The above is the most basic way to make it work.