gcc to compile Java programs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Hi,

I had downloaded and built the gcc-4.6.1 on my ubuntu linux. I had enabled the default languages during the configuration. So I have support for Java.

I have a simple HelloWorld Java program.

 public class HelloWorld {
    public static void main(String [] args) {
      System.out.println("Hello");
    }
  }


I am able to use the gcj driver program to compile the HelloWorld.java to generate a binary using the command
gcj HelloWorld.java --main=HelloWorld -o HelloWorld -static -v 

However, when I try to use gcc as the driver it is not able to build a binary. After observing the options used by gcj driver, I was able to get get it past the compilations and assembly by supplying a huge number of options.

gcc -fsaw-java-file -v -fhash-synchronization -fno-use-divide-subroutine -fuse-boehm-gc -fnon-call-exceptions -fkeep-inline-functions -g1 -fbootclasspath=/home/rags/my_gcc/gcc-4.6.1/share/java/:./:/home/rags/my_gcc/gcc-4.6.1/share/java/libgcj-4.6.1.jar -o HelloWorld -static -L/home/rags/my_gcc/gcc-4.6.1/lib/gcc/i686-pc-linux-gnu/4.6.1 -L/home/rags/my_gcc/gcc-4.6.1/lib/gcc/i686-pc-linux-gnu/4.6.1/../../.. -lgcc -lgcc_eh -lgcj -lm -lrt -ldl -lgcc -lgcc_eh -lc -lpthread HelloWorld.java

However, it fails at the link time citing that it is not able to find 'main'.

/usr/lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/cc21fQ6Y.o: In function `HelloWorld::HelloWorld()':
ccX4hNPt.jar:(.text+0x13): undefined reference to `java::lang::Object::Object()'
/tmp/cc21fQ6Y.o: In function `void HelloWorld::main(JArray<java::lang::String*>*)':
ccX4hNPt.jar:(.text+0x2e): undefined reference to `_Jv_InitClass'
ccX4hNPt.jar:(.text+0x39): undefined reference to `java::lang::System::class$'
ccX4hNPt.jar:(.text+0x3e): undefined reference to `_Jv_InitClass'
ccX4hNPt.jar:(.text+0x48): undefined reference to `java::lang::System::out'
/tmp/cc21fQ6Y.o: In function `_Jv_global_static_constructor':
ccX4hNPt.jar:(.text+0x7c): undefined reference to `_Jv_RegisterResource'
/tmp/cc21fQ6Y.o:(.data+0x10): undefined reference to `void java::lang::Object::finalize()'
/tmp/cc21fQ6Y.o:(.data+0x14): undefined reference to `int java::lang::Object::hashCode()'
/tmp/cc21fQ6Y.o:(.data+0x18): undefined reference to `bool java::lang::Object::equals(java::lang::Object*)'
/tmp/cc21fQ6Y.o:(.data+0x1c): undefined reference to `java::lang::String* java::lang::Object::toString()'
/tmp/cc21fQ6Y.o:(.data+0x20): undefined reference to `java::lang::Object* java::lang::Object::clone()'
/tmp/cc21fQ6Y.o:(.data+0x24): undefined reference to `void java::lang::Object::throwNoSuchMethodError()'
/tmp/cc21fQ6Y.o:(.data+0xa0): undefined reference to `vtable for java::lang::Class'
/tmp/cc21fQ6Y.o:(.data+0xb0): undefined reference to `java::lang::Object::class$'
collect2: ld returned 1 exit status


I see that the gcj uses the jvgenmain program to generate the main() program.
/home/rags/my_gcc/gcc-4.6.1/libexec/gcc/i686-pc-linux-gnu/4.6.1/jvgenmain HelloWorldmain /tmp/ccYiuAkp.i

The gcc driver is NOT invoking jvgenmain program to have the main() and hence it is not able to find the main()

My question is 
(1) Is gcj the only way to compile java programs ? . 
(2) Can we use the gcc driver to compile and link standalone Java programs ? What is the option that I am missing for gcc to compile my HelloWorld Program ?

Thanks.

Bye,
Raghavan V



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux