Differences

This shows you the differences between two versions of the page.

Link to this comparison view

gcj [2012/11/04 21:01]
gcj [2022/04/16 12:23] (current)
Line 1: Line 1:
 +@related: [[Java]] [[gcc]] [[Compiler]] [[TuxGuitar]] [[jdk]]
  
 +
 +==== [[Libre]] [[Java]] [[Compiler]] : gcj ====
 +
 +  * http://gcc.gnu.org/java
 +  * irc://irc.oftc.net/#gcj
 +  * irc://irc.freenode.net/#classpath.
 +
 +[[ToDo]] : http://lists.debian.org/debian-gcc/2005/04/msg00020.html
 +
 +<code>
 +  mkdir -p gcjtest ; cd gcjtest
 +  cat > Test.java << EOF
 +  class Test {
 +   public static int f(int in) {
 +       System.out.println("#{ Test.java::f");
 +       System.out.println("#" + in );
 +       System.out.println("#} Test.java::f");
 +       return in;
 +   }
 +   // public static void fvoid() { } // jni bug to check?
 +  }
 +EOF
 +
 +  cat > Main.java <<EOF
 +  class Main {
 +   public static void main(String[[]] args) {
 +       System.out.println("#{ Main.java::main");
 +       Test.f ( args.length );
 +       System.out.println("#} Main.java::main");
 +   }
 +  }
 +EOF
 +</code>
 +
 +
 +On [[Linux]] :
 +
 +  # $ make
 +  gcj -C --classpath=. Test.java
 +  gcj -c  -o Test.o --classpath=. Test.java
 +  libtool --mode=link --tag=GCJ gcj --shared Test.o  -o libgcjtest.so
 +  gcj -c  -o Main.o --classpath=. Main.java
 +  gcj -o main --main=Main --classpath=. Main.o -lgcjtest -L. -lgcj
 +
 +  ldd main
 +  #       libgcjtest.so => ./libgcjtest.so (0xb7fe7000)
 +  #       libgcj.so.3 => /usr/lib/./libgcj.so.3 (0xb79f4000)
 +  #       libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb79d8000)
 +  #       libm.so.6 => /lib/tls/libm.so.6 (0xb79b6000)
 +  #       libpthread.so.0 => /lib/tls/libpthread.so.0 (0xb79a7000)
 +  #       libz.so.1 => /usr/lib/./libz.so.1 (0xb7995000)
 +  #       libdl.so.2 => /lib/tls/libdl.so.2 (0xb7992000)
 +  #       libc.so.6 => /lib/tls/libc.so.6 (0xb785d000)
 +  #       /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0xb7fea000)
 +
 +  LD_LIBRARY_PATH=. && ./main *
 +  #{ Main.java::main
 +  #{ Test.java::f
 +  #7
 +  #} Test.java::f
 +  #} Main.java::main
 +
 +==== On [[Cygwin]] : ====
 +
 +  gcj -C --classpath=. Test.java
 +  gcj -c  -o Test.o --classpath=. Test.java
 +  gcj -Wl,-Bsymbolic -shared -fPIC -fjni -findirect-dispatch Test.o  -o libgcjtest.so # ok [[Cygwin]]
 +  gcj -c  -o Main.o --classpath=. Main.java
 +  gcj -o main --main=Main --classpath=. Main.o -lgcjtest -L. -lgcj
 +  # h:\usr\local\WIN32\opt\thisiscool-gcc\gcc-3.4\bin\..\lib\gcc\i686-pc-mingw32\3.4.0\..\..\..\..\i686-pc-mingw32\bin\ld.exe:
 +  # cannot find -lgcjtest
 +
 +http://www.thisiscool.com/gcc_mingw.htm
 +
 +
 +gcj works pretty fine, unless when I tried to make a shared library (.so) under
 +[[Cygwin]], this code works flawlessly w/ gcc 3.4 [[Linux]] [[Debian]]
 +
 +you wrote a warning on gcc 4.0 but this also happend on 3.4 (see code)
 +
 +
 +==== [[C++]] ====
 +
 +http://gcc.gnu.org/onlinedocs/gcj/Invocation.html#Invocation
 +
 +<code>
 +  cat > main-cpp.cpp << EOF
 +  #include <gcj/cni.h>
 +  #include <java/lang/System.h>
 +  #include <java/io/PrintStream.h>
 +  #include <java/lang/Throwable.h>
 +
 +  int main(int argc, char *argv) {
 +       using namespace java::lang;
 +
 +       try {
 +         JvCreateJavaVM(NULL);
 +         JvAttachCurrentThread(NULL, NULL);
 +
 +         String *message = JvNewStringLatin1("Hello from C++");
 +         JvInitClass(&System::class$);
 +         System::out->println(message);
 +
 +         JvDetachCurrentThread();
 +       } catch (Throwable *t) {
 +         System::err->println(JvNewStringLatin1("Unhandled Java exception:"));
 +         t->printStackTrace();
 +       }
 +     }
 +EOF
 +</code>
 +
 +  make CXX=gcj LDFLAGS=-lgcj main-cpp  && ./main-cpp
 +
 +==== [[C++]] call a dynamic native [[Java]] library ====
 +
 +<code>
 +  cat > main-test-cpp.cpp << EOF
 +  #include <gcj/cni.h>
 +  #include <java/lang/System.h>
 +  #include <java/io/PrintStream.h>
 +  #include <java/lang/Throwable.h>
 +  #include "Test.h"
 +
 +  int main(int argc, char *argv) {
 +       using namespace java::lang;
 +
 +       try {
 +         JvCreateJavaVM(NULL);
 +         JvAttachCurrentThread(NULL, NULL);
 +
 +         String *message = JvNewStringLatin1("Hello from C++");
 +         JvInitClass(&System::class$);
 +         System::out->println(message);
 +
 +        ::Test::f( argc );
 +
 +        JvDetachCurrentThread();
 +       } catch (Throwable *t) {
 +         System::err->println(JvNewStringLatin1("Unhandled Java exception:"));
 +         t->printStackTrace();
 +       }
 +     }
 +EOF
 +</code>
 +
 +  make CXX=gcj LDFLAGS="-lgcj -lgcjtest -L." main-test-cpp  && ./main-test-cpp *
 +
 +  Hello from C++
 +  #{ Test.java::f
 +  #10
 +  #} Test.java::f
 +
 +
 +  ldd main-test-cpp
 +        libgcj.so.3 => /usr/lib/libgcj.so.3 (0xb79e4000)
 +        libgcjtest.so => ./libgcjtest.so (0xb79e1000)
 +        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb79d8000)
 +        libm.so.6 => /lib/tls/libm.so.6 (0xb79b6000)
 +        libpthread.so.0 => /lib/tls/libpthread.so.0 (0xb79a7000)
 +        libz.so.1 => /usr/lib/libz.so.1 (0xb7995000)
 +        libdl.so.2 => /lib/tls/libdl.so.2 (0xb7992000)
 +        libc.so.6 => /lib/tls/libc.so.6 (0xb785d000)
 +        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0xb7fea000)
 +
 +
 +==== GCJH ====
 +
 +<code>
 +cat Test.java
 +class Test
 +{
 +    static final String FILE = new String("Test.java");
 +
 +    public static void test()
 +    {
 +        System.out.println( FILE + ":0: log: #{ " + "Test::test");
 +        System.out.println( FILE + ":0: log: #} " + "Test::test");
 +    }
 +
 +    public static void main(String[] args)
 +    {
 +        System.out.println( FILE + ":0: log: #{ " + "Test::main");
 +        test();
 +        System.out.println( FILE + ":0: log: #} " + "Test::main");
 +    }
 +
 +}
 +gcj -c  -o Test.class.o --classpath=. Test.java
 +libtool --mode=link --tag=GCJ gcj --shared  -o libTest.jar.so Test.class.o
 +libtool: link: gcj --shared -o libTest.jar.so Test.class.o 
 +javac Test.java
 +gcjh Test.class -o Test.java.h
 +cat Test.java.h
 +
 +// DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-
 +
 +#ifndef __Test__
 +#define __Test__
 +
 +#pragma interface
 +
 +#include <java/lang/Object.h>
 +#include <gcj/array.h>
 +
 +extern "Java"
 +{
 +    class Test;
 +}
 +
 +class Test : public ::java::lang::Object
 +{
 +
 +public: // actually package-private
 +  Test();
 +public:
 +  static void test();
 +  static void main(JArray< ::java::lang::String * > *);
 +public: // actually package-private
 +  static ::java::lang::String * FILE;
 +public:
 +  static ::java::lang::Class class$;
 +};
 +
 +#endif // __Test__
 +cat main.cpp
 +#include <gcj/cni.h>
 +#include <java/lang/System.h>
 +#include <java/io/PrintStream.h>
 +#include <java/lang/Throwable.h>
 +#include "Test.java.h"
 +
 +int main(int argc, char *argv[])
 +{
 +  using namespace java::lang;
 +
 +  try {
 +    JvCreateJavaVM(NULL);
 +    JvAttachCurrentThread(NULL, NULL);
 +
 +    String *message = JvNewStringLatin1( __FILE__ ":" "0" ": log:" );
 +    JvInitClass(&System::class$);
 +    System::out->println(message);
 +
 +    ::Test::test();
 +
 +    //::Test::main();
 +
 +    JvDetachCurrentThread();
 +  } catch (Throwable *t) {
 +    System::err->println(JvNewStringLatin1("Unhandled Java exception:"));
 +    t->printStackTrace();
 +  }
 +}
 +gcj -L. -lgcj -lTest.jar -o main.exe main.cpp
 +cc1plus: warning: command line option "-fbootclasspath=./:/usr/share/java/libgcj-4.4.jar" is valid for Java but not for C++
 +LD_LIBRARY_PATH=. ./main.exe
 +main.cpp:0: log:
 +Test.java:0: log: #{ Test::test
 +Test.java:0: log: #} Test::test
 +</code>
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki