This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

g++ 4.1 compiling against libgcj; classes cannot inherit from Object?


I'm trying to make a simple hello world program wherein I have a C++
object that inherits from java::lang::Object.  When I try to compile
the program with g++, it gives me the following error:

$ make
/opt/gcc-4.1/bin/g++ -o hello hello.java.cpp -L/opt/gcc-4.1/lib -lgcj
/tmp/ccSrbVBE.o: In function `hidden alias for Hello::Hello()':
hello.java.cpp:(.gnu.linkonce.t._ZN5HelloC1Ev+0xd): undefined
reference to `java::lang::Object::Object()'
hello.java.cpp:(.gnu.linkonce.t._ZN5HelloC1Ev+0x12): undefined
reference to `vtable for Hello'
collect2: ld returned 1 exit status
make: *** [hello] Error 1

So, it looks like it can't find Object::Object(), but when I do nm -C
/opt/gcc-4.1/lib/libgcj.so, I get

...
006b1e40 T java::lang::Object::Object()
...

So, it looks like the Object contructor is there.  Does anyone know
what I have to do to inherit from Object with a c++ class?  The docs
imply that it's possible, but I can't make it work.  The complete
source follows:

hello.java.cpp:

#include <gcj/cni.h>
#include <java/lang/Object.h>
#include <java/lang/System.h>
#include <java/lang/String.h>
#include <java/io/PrintStream.h>
#include <java/lang/Throwable.h>

class Hello : public java::lang::Object {
  public:
  static java::lang::Class class$;

  Hello() {
    JvInitClass(&class$);
  }

  java::lang::String* sayHello() {
    using namespace java::lang;
    String *message = JvNewStringLatin1("Hello from C++");
    JvInitClass(&System::class$);
    System::out->println(message);
    return message;
  }
};

java::lang::Class Hello::class$;

int main() {
  try
  {
    JvCreateJavaVM(0);
    JvAttachCurrentThread(0, 0);

    Hello* hello = new Hello;
    hello->sayHello();
    JvDetachCurrentThread();
  }
  catch (java::lang::Throwable *t)
  {
    java::lang::System::err->println(
        JvNewStringLatin1("Unhandled Java exception:"));
    t->printStackTrace();
  }
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]